标签:init 目录 for循环 oca lis local 命令 exit seq命令
for循环语句列表形式有:
[root@localhost ~]# vim 1.sh #!/bin/bash for i in 1 2 3 4 //在命令中定义列表 do echo $i done [root@localhost ~]# sh 1.sh 1 2 3 4
[root@localhost ~]# vim 2.sh #!/bin/bash for i in {1..5} do echo $i done [root@localhost ~]# sh 2.sh 1 2 3 4 5
使用{初始值..结束值}来表示一个区间的数值
[root@localhost ~]# vim 3.sh #!/bin/bash list="1 2 3 4" for i in $list do echo $i done [root@localhost ~]# sh 3.sh 1 2 3 4
[root@localhost ~]# vim 4.sh #!/bin/bash for i in `seq 1 1 5` do echo $i done [root@localhost ~]# sh 4.sh 1 2 3 4 5
使用seq命令生成列表,seq命令的格式为:seq 初始值 递增值 终止值
[root@localhost ~]# vim 5.sh #!/bin/bash for i in /root/* do if [ -d "$i" ] then echo "$i is a directory" elif [ -f "$i" ] then echo "$i is a file" fi done [root@localhost ~]# sh 5.sh /root/5.sh is a file /root/a is a directory /root/anaconda-ks.cfg is a file /root/a.sh is a file /root/initial-setup-ks.cfg is a file
利用for循环语句计算出“1+……+100”的值
[root@localhost ~]# vim 6.sh #!/bin/bash sum=0 for ((i=1;i<=100;i++ )) do let sum+=$i done echo "1+..+100="$sum [root@localhost ~]# sh 6.sh 1+..+100=5050
利用while循环语句计算出“1+……+100”的值
[root@localhost ~]# vim 7.sh #!/bin/bash i=0 sum=0 while [ $i -le 100 ] do let sum+=$i let i++ done echo "1+..+100="$sum [root@localhost ~]# sh 7.sh 1+..+100=5050
[root@localhost ~]# vim 8.sh #!/bin/bash for i in 1 2 3 4 5 do if [ $i -eq 3 ] then break fi echo $i done [root@localhost ~]# sh 8.sh 1 2
* **break:**结束并退出循环
* **continue:**在循环中不执行continue下面的代码,转而进入下一轮循环
* **exit:**退出脚本,常带一个整数给系统,如 exit 0
标签:init 目录 for循环 oca lis local 命令 exit seq命令
原文地址:https://blog.51cto.com/14157628/2425658