bash脚本编程---循环
单分支的if语句:
if 测试语句then代码分支fi双分支的if语句:
if 测试条件;then条件为真时执行的分支else条件为假时执行的分支fi
例1:通过参数传递一个用户名给脚本,此用户不存时,则添加之;
#!/bin/bashif ! (grep "^$1\>" /etc/passwd &> /dev/null);thenuseradd $1echo $1 | passwd --stdin $1 &>/dev/nullecho "add user $1 finished."elseecho "the user $1 is exists."fi- #如果给出的用户不存在,就添加,用户帐号与密码相同,如果存在就提示存在
#!/bin/bashif [ $# -eq 1 ];thenecho "At least one username."exit 2fi#先判断是否输入了一个参数,如果没有或者输入了多个参数就退出#如果是“-lt”,则表示最少一个参数,即使输入多个也只取第一个if ! (grep "^$1\>" /etc/passwd &> /dev/null);thenuseradd $1echo $1 | passwd --stdin $1 &>/dev/nullecho "add user $1 finished."elseecho "the user $1 is exists."fi
#!/bin/bashif [ $# -lt 2 ];thenecho "Must give two num."exit 2fiif [ $1 -ge $2 ];thenecho "Max num is $1."elseecho "Max num is $2."fi
#!/bin/bashif [ $# -lt 2 ];thenecho "Must give two num."fideclare -i max=$1if [ $1 -lt $2 ];thenmax=$2fiecho "The max num is $max"#先对max进行赋值,然后进行比较
#!/bin/bashif [ $# -lt 1 ];thenecho "Must give a username."finum=$(id -u $1)let num2=$num%2#echo "num is $num"#echo "num2 is $num2"if [ $num2 -eq 1 ];thenecho "the uid is ji"elseecho "the uid is ou"fi
#!/bin/bashif [ $# -lt 2 ];thenecho "Must give two filename."exit 2fiif [ -f $1 ];thennum1=$(cat $1 |wc -l)echo "the file $1 is exists and hangshu is $num1."elseecho "the file $1 is not exists."exit 4fiif [ -f $2 ];thennum2=$(cat $2 |wc -l)echo "the file $2 is exists and hangshu is $num2."elseecho "the file $2 is not exists."exit 3fiif [ $num1 -lt $num2 ];thenecho "$2 hangshu is more."elseecho "$1 hangshu us more."
1.bash编程之选择执行--if语句
1.1 单分支if语句
if 测试条件;then为真时执行;fi
1.2 双分支if语句
if 测试条件;then为真时执行else为假时执行fi
1.3 多分支语句
if 测试条件;then条件1为真时执行elif condition-2;then条件2为真时执行elif condition-3;then条件3为真时执行。。。。elif condition-n;then条件n为真时执行else所有条件都不满足时执行的分支fi
#!/bin/bash#if [ $# -lt 1 ];thenecho "Must give a path."exit 2fiif [ -L $1 ];thenecho "Symbolic link."elif [ -b $1 ];thenecho "block special."elif [ -c $1 ];thenecho "Character special file."elif [ -S $1 ];thenecho "Socket file."elif [ -f $1 ];thenecho "common file."elif [ -d $1 ];thenecho "Directory."lseecho "UNKNOW."fi
#!/bin/bash[ $# -lt 1 ] && echo "At least one username."&& exit 1 #先判断参量是否存在! id $1 &>/dev/null && echo "Bo such user."&& exit 2 #判断用户是否存在uid=$(id -u $1)if [ $uid -eq 0 ];thenecho "the user is root."elif [ $uid -le 1000 ];thenecho "the user is system user."elseecho "the user is login user."fi
2.bash编程之循环执行
2.1 for循环
for VARIABLE in LIST;do循环体done
(a):{start..end}(b):seq [start [incremtal]] last
3.返回列表的命令
2.2 while循环
while CONDITION;do循环体循环控制变量修正表达式done
2.3 until循环
until CONDITION;do循环体循环控制变量修正表达式done
#!/bin/bashdeclare -i sum=0declare -i i=1#until [ $i -gt 100 ];do#判断i的值是否大于100,为假时循环while [ $i -le 100 ];do#判断i的值是否小于等于100,为真时循环let sum+=$ilet i++doneecho $sum
#!/bin/bashdeclare -i sum=0#for i in {1..100};dofor i in `seq 1 100`;dolet sum=$sum+$idoneecho "sum=$sum"
#!/bin/bashfor i in {101..103};doname="user${i}"! id $name &> /dev/null && useradd $name && echo "add user ${name} succeed!"echo "$i" | passwd --stdin "$name" &> /dev/null && echo "set passwd '${name}' for $name succeed!"done
#!/bin/bashfor j in {1..9};dofor i in `seq 1 $j`;doecho -n -e "${i}x${j}=$[${i}*${j}]\t"doneechodone
#!/bin/bashfor s in {1..9};doj=$[10-$s]for i in $(seq 1 $j);doecho -n -e "${i}X${j}=$[${i}*${j}]\t"doneechodone
#!/bin/bashdeclare -i s=9until [ $s -eq 0 ];dodeclare -i j=1#注意:每次循环,i的值都会发生变化,一定要重新定义until [ $j -gt $s ];doecho -n -e "${s}X${j}=$[${s}*${j}]\t"let j++done- let s--
echodone
#!/bin/bashdeclare -i s=9while [ $s -gt 0 ];dodeclare -i j=1while [ $j -le $s ];doecho -n -e "${s}X${j}=$[${s}*${j}]\t"let j++donelet s--echodone
bash脚本编程---循环的更多相关文章
- Bash脚本编程学习笔记07:循环结构体
本篇中涉及到算术运算,使用了$[]这种我未在官方手册中见到的用法,但是确实可用的,在此前的博文<Bash脚本编程学习笔记03:算术运算>中我有说明不要使用,不过自己忘记了.大家还是尽量使用 ...
- 脚本命令高级Bash脚本编程指南(31):数学计算命令
题记:写这篇博客要主是加深自己对脚本命令的认识和总结实现算法时的一些验经和训教,如果有错误请指出,万分感谢. 高等Bash脚本编程指南(31):数学盘算命令 成于坚持,败于止步 操作数字 factor ...
- Bash脚本编程总结
bash脚本编程之用户交互: read [option]… [name …] -p ‘PROMPT’ -t TIMEOUT bash -n /path/to/some_script 检测脚本中的 ...
- bash脚本编程知识储备
bash脚本编程: 脚本程序:解释器解释执行: 首先得理清一些琐碎的知识点,我尽量把我所学的帮朋友一起梳理一下 编程环境:(我会在接下来的篇章,图文例子三结合的方式带大家一起学习) ...
- Bash脚本编程之算术运算
简介 Bash所支持的算术运算和C语言是一样的,这里指的是操作符(operator)以及它们的优先级(precedence).结合性(associativity)和值,详见Shell Arithmet ...
- Bash脚本编程之数组
数组简介 在bash脚本编程当中,变量是存储单个元素的内存空间:而数组是存储多个元素的一段连续的内存空间. 数组由数组名和下标构成,如下. ARRAY_NAME[SUBSCRIPT] 数组按照下标的类 ...
- Bash脚本编程学习笔记06:条件结构体
简介 在bash脚本编程中,条件结构体使用if语句和case语句两种句式. if语句 单分支if语句 if TEST; then CMD fi TEST:条件判断,多数情况下可使用test命令来实现, ...
- Bash 脚本编程
概述 Bash (GNU Bourne-Again Shell) 是许多Linux发行版的默认Shell. shell语法 变量 定义:your_name="hellohhy" 使 ...
- 高级Bash脚本编程指南(27):文本处理命令(三)
高级Bash脚本编程指南(27):文本处理命令(三) 成于坚持,败于止步 处理文本和文本文件的命令 tr 字符转换过滤器. 必须使用引用或中括号, 这样做才是合理的. 引用可以阻止shell重新解释出 ...
随机推荐
- Xamarin控件使用之GridView
[Activity(Label = "MainGridViewActivity", LaunchMode = LaunchMode.SingleTop)]//设置Activity启 ...
- 备份Rhythmbox播放器的曲目和播放列表信息
Rhythmbox音乐播放器只能保存单个播放列表,如果在rhythmbox下建了很多播放列表(比如按歌手名分类),每个播放列表下包含一些歌曲,为了避免重装系统后重新建这些播放列表,可以备份下面的文件. ...
- 关于通用的C#后台获取前台页面的标签的正则表达式
Regex reg = new Regex("<div[^>]*?class=\"类属性名称\"[^>]*>(.*?) </div> ...
- /dev/shm 与 tmpfs
1./dev/shm 与 tmpfs /dev/shm/是linux下一个目录,/dev/shm目录不在磁盘上,而是在内存里, 类型为 tmpfs ,因此使用linux /dev/shm/ 的效率非常 ...
- 深入浅出 spring-data-elasticsearch - 基本案例详解(三
『 风云说:能分享自己职位的知识的领导是个好领导. 』运行环境:JDK 7 或 8,Maven 3.0+技术栈:SpringBoot 1.5+, Spring Data Elasticsearch ...
- 短信发送接口被恶意访问的网络攻击事件(三)定位恶意IP的日志分析脚本
前言 承接前文<短信发送接口被恶意访问的网络攻击事件(二)肉搏战-阻止恶意请求>,文中有讲到一个定位非法IP的shell脚本,现在就来公布一下吧,并没有什么技术难度,只是当时花了些时间去写 ...
- 逃跑(escape)
逃跑(escape) 时间限制: 3 Sec 内存限制: 128 MB 题目描述 输入 第一行是5个正整数,n,m,k,S,T,分别代表无向图点数,边数,蝙蝠的数量,二小姐所在起点的编号,目标点的编 ...
- Mac 使用ab命令进行压测
Mac 使用ab命令进行压测 1.在Mac中配置Apache Mac中应该有自带了Apache,详细配置见http://www.cnblogs.com/snandy/archive/2012/11/1 ...
- 九度OJ:1002-Grading
时间限制:1 秒内存限制:32 兆特殊判题:否提交:24102解决:6126 题目描述: Grading hundreds of thousands of Graduate Entrance Exam ...
- HTML行内元素、块状元素、行内块状元素的区别
HTML可以将元素分类方式分为行内元素.块状元素和行内块状元素三种.首先需要说明的是,这三者是可以互相转换的,使用display属性能够将三者任意转换: (1)display:inline;转换为行内 ...