#===========》Shell风格语法 for 变量名 [ in 取值列表 ] do 循环体 done #===========》C语言风格语法 for ((初值;条件;步长)) do 循环体 done
shell风格的for,常用in列表方式
for i in 1 2 3 for i in {1,2,3} for i in {1..9} for i in {9..1} for i in {a..z} for i in {A..Z} for i in {X..Z} for i in $(命令) # 例如:for i in $(head -10 /etc/passwd);do echo $i|cut -d: -f1,2;done for i in $(find ...)
continue与for
continue:默认退出本次循环 break:默认退出本层循环
案例1:shell风格的for
for i in {1..10} do echo $i done
案例2:c语言风格的for
for ((i=1;i<=10;i++)) do echo $i done
案例3:检查内网存活的ip,使用&符号提升脚本的运行效率
#!/bin/bash for i in {1..254} do (ping -W 1 -c 1 192.168.12.$i &> /dev/null && echo 192.168.12.$i) & done
案例4:编写文件类型测试脚本,支持多个参数
[root@egon /]# cat file.sh #!/bin/bash for i in $@ do if [[ -d $i ]];then echo "$i is directory." elif [[ -b $i ]];then echo "$i is block device." elif [[ -f $i ]];then echo "$i is a regular file." else echo "unknow." fi done [root@egon /]# chmod +x file.sh [root@egon /]# [root@egon /]# ./file.sh a.txt /etc /root /abc a.txt is a regular file. /etc is directory. /root is directory. unknow.
案例5:可以直接在命令行编写for循环
[root@egon /]# for i in {1..10};do [ $i -eq 5 ] && continue || echo $i;done [root@egon /]# for i in {1..10};do [ $i -eq 5 ] && break || echo $i;done
案例6:统计dev下每种文件类型的数量
#!/bin/bash dir='/dev' for i in `ls $dir` do if [ -b $dir/$i ];then ((block++)) # 或者 let block++,下同 elif [ -f $dir/$i ];then ((file++)) elif [ -d $dir/$i ];then ((directory++)) else ((unkown++)) fi done echo 'block' $block echo 'regular file' $file echo 'directory' $directory echo 'unkown' $unkown
案例7:向脚本传递一个用户名,验证这个用户是否存在.
[root@egon ~]# cat testuser.sh #!/bin/bash id $1 &> /dev/null if [ $? -eq 0 ];then echo "用户$1存在" else echo "用户$1不存在" fi [root@egon ~]# ./testuser.sh root 用户root存在
案例8:添加30个用户,再将它们删除
for i in {1..30}; do useradd user$i&&echo "user$i create successful" done for i in {1..30}; do userdel -r user$i&&echo "user$i delete successful" done
案例9:输入账号信息,输入个数,批量创建用户user01、user02、user03...,密码默认123456
[root@egon shell]# cat adduser.sh #!/bin/bash read -p "请输入创建的用户名信息: " name read -p "请输入创建的用户数量: " count for i in `seq -w $count` do echo $name$i useradd $name$i &>/dev/null echo 123456 | passwd --stdin $name$i &>/dev/null id $name$i &>/dev/null [ $? -eq 0 ] && echo "$name$i create is ok" || echo "$name$i create is failed" done
案例10:嵌套多层for循环,结合break与continue,(了解即可)
#1、使用break: break 默认参数是 1 所以写 break 等于 break 1 意义:退出当前循环层 break 2 则向上退出2层循环 当前循环也计算在退出层次里 # 示例 for i in {0..3} do echo -e "第一层循环:loop$i" for j in {0..3} do echo -e "\t第二层循环:loop$j" for n in {0..3} do echo -e "\t\t第三层循环:loop$n:$i$j$n" if ((n==2));then break 3 fi done done done #2、使用continue continue = continue 1 在当次循环中忽略continue后续的代码 就是:立即结束当前循环中的当次循环,而转入当前循环的下一次循环 continue 2 等同于 break 1 continue 3 等同于 break 2 总结:continue n 等同于 break n-1 for i in {0..3} do echo -e "第一层循环:loop$i" for j in {0..3} do echo -e "\t第二层循环:loop$j" for n in {0..3} do echo -e "\t\t第三层循环:loop$n:$i$j$n" if ((n==2));then continue 3 fi done done done
select表达式是bash的一种扩展应用,擅长于交互式场合。用户可以从一组不同的值中进行选择
select var in ... do ... break done
案例1:
[root@egon /]# cat select.sh #!/bin/bash PS3='choose one: ' # select默认使用PS3变量的值做提示符 echo select var in {A..D} do echo echo "your choose is $var" echo "OK" echo break # 跳出select,否则是死循环 done [root@egon /]# [root@egon /]# ./select.sh 1) A 2) B 3) C 4) D choose one: 3 your choose is C OK
案例2:若省略 in list 则select会把 $@ 当做列表项
[root@egon /]# cat select.sh #!/bin/bash PS3='choose one: ' # select默认使用PS3变量的值做提示符 echo select var do echo echo "your choose is $var" echo "OK" echo break # 跳出select,否则是死循环 done [root@egon /]# ./select.sh 苹果 梨 蔬菜 香蕉 茄子 1) 苹果 2) 梨 3) 蔬菜 4) 香蕉 5) 茄子 choose one: 1 your choose is 苹果 OK