20.10 for 循环
语法: for 变量名 in 条件; do ...; done
- #!/bin/bash
- for i in `seq 1 100`
- do
- echo $i
- done
案例 1
- #!/bin/bash
- sum=0
- for i in `seq 1 100`
- do
- sum=$[$sum+$i]
- echo $i
- done
- echo $sum
- [root@aiker02 script]# !vim
- vim for0.sh
- echo $i
- #!/bin/bash
- sum=0
- for i in `seq 1 100`
- do
- sum=$[$sum+$i]
- done
- echo $sum
- "for0.sh" 7L, 73C written
- [root@aiker02 script]# sh -x !$
- sh -x for0.sh
- + echo 5050
- 5050
- [root@aiker02 script]# cat !$
- cat for0.sh
- #!/bin/bash
- sum=0
- for i in `seq 1 100`
- do
- echo "$sum+$i"
- sum=$[$sum+$i]
- echo $sum
- done
- [root@aiker02 script]# !v
- vim for1.sh
- #!/bin/bash
- cd /etc/
- for a in `ls /etc`
- do
- [ -d $a ] && ls $a
- if [ -d $a ]
- then
- echo $a
- echo "`pwd`/$a"
- ls $a
- fi
- done
- [root@aiker02 script]# for i in `seq 1 3`; do echo $i; done
- 1
- 2
- 3
- [root@aiker02 script]# for i in 1 2 3; do echo $i; done
- 1
- 2
- 3
注意 for 在遍历目录时会把空格或者回车作为分隔符
- [root@aiker02 script]# ll
- total 64
- -rw-r--r-- 1 root root 0 Apr 15 16:27 1
- -rw-r--r-- 1 root root 0 Apr 15 16:27 2
- -rw-r--r-- 1 root root 0 Apr 15 16:27 3 4.txt
- [root@aiker02 script]# mkdir textdir
- [root@aiker02 script]# mv 1 2 3\ 4.txt textdir/
- 1234.txt
- [root@aiker02 script]# for i in `ls ./textdir`; do echo $i; done
- 1
- 2
- 3
- 4.txt
- [root@aiker02 script]# for i in `ls ./textdir`; do printf "$i\n"; done
- 1
- 2
- 3
- 4.txt
通常情况下 shell 变量调用需要加 $, 但是 for 的 (()) 中不需要, 下面来看一个例子:
- #!/bin/bash
- for((i=1;i<=5;i++));do
- echo "这是第 $i 次调用";
- done;
与 C 中相似, 赋值和下一步执行可以放到代码之前循环语句之中执行, 这里要注意一点: 如果要在循环体中进行 for 中的 next 操作, 记得变量要加 $, 不然程序会变成死循环.
20.11/20.12 while 循环
语法 while 条件; do ... ; done
案例 1
- #!/bin/bash
- while : ##: 相当于 true, 可以使用 while true
- do
- load=`w|head -1|awk -F 'load average:' '{print $2}'|cut -d. -f1`
- if [ $load -gt 10 ]
- then
- /bin/mail.py aikeav@qq.com "load is high" $load
- fi
- sleep 30
- done
- [root@aiker02 script]# sh -x while0.sh
- + :
- ++ head -1
- ++ awk -F 'load average:' '{print $2}'
- ++ cut -d. -f1
- ++ w
- + load=0
- + '[' 0 -gt 10 ']'
- + sleep 30
- + :
- ++ head -1
- ++ awk -F 'load average:' '{print $2}'
- ++ cut -d. -f1
- ++ w
- + load=0
- + '[' 0 -gt 10 ']'
- + sleep 30
- + :
- ++ head -1
- ++ awk -F 'load average:' '{print $2}'
- ++ cut -d. -f1
- ++ w
- + load=0
- + '[' 0 -gt 10 ']'
- + sleep 30
- ^C
while 循环: 格式:
- while command
- do
- command
- command
- command
- ...
- done
- [root@aiker02 script]# !vim
- vim while1.sh
- #!/bin/bash
- while :
- do
- read -p "please input a number:" n
- if [ -z $n ]
- then
- echo 'You need input a string.'
- continue
- fi
- n1=`echo $n | sed 's/[0-9]//g'`
- if [ -n "$n1" ]
- then
- echo "You must input numbers"
- continue
- fi
- break
- done
- echo $n
- [root@aiker02 script]# !sh
- sh -x while1.sh
- + :
- + read -p 'please input a number:' n
- please input a number:12323
- + '[' -z 12323 ']'
- ++ sed 's/[0-9]//g'
- ++ echo 12323
- + n1=
- + '[' -n ''']'
- + break
- + echo 12323
- 12323
- [root@aiker02 script]# !vim
- vim while2.sh
- #!/bin/bash
- i=1
- while [ $i -le 5 ]
- do sq=`expr $i \* $i`
echo $i $i 的平方是 $sq
- i=`expr $i + 1`
- done
- echo "Job is done."
- [root@aiker02 script]# sh !$
- sh while2.sh
1 1 的平方是 1
2 2 的平方是 4
3 3 的平方是 9
4 4 的平方是 16
5 5 的平方是 25
Job is done.
20.13 break 跳出循环
? 从循环中退出: break 和 continue 命令 break 立即退出循环 continue 忽略本循环中的其他命令, 继续下一下循环 在 shell 编程中有时我们要用到进行无限循环的技巧, 也就是说这种循环一直执行碰 到 break 或 continue 命令. 这种无限循环通常是使用 true 或 false 命令开始的. UNIX 系统中的 true 总是返 0 值, 而 false 则返回非零值. 如下所示
- while true until false
- do do
- command command
- .... ....
- command command
- done done
break 命令允许跳出所有循环 (终止执行后面的所有循环)
- [root@aiker02 script]# vim break.sh
- #!/bin/bash
- for i in `seq 1 5`
- do
- echo $i
- if [ $i -eq 3 ]
- then
- break
- fi
- echo $i
- done
- echo "job is done"
- [root@aiker02 script]# sh break.sh
- 1
- 1
- 2
- 2
- 3
- job is done
- [root@aiker02 script]# sh -x break.sh
- ++ seq 1 5
- + for i in '`seq 1 5`'
- + echo 1
- 1
- + '[' 1 -eq 3 ']'
- + echo 1
- 1
- + for i in '`seq 1 5`'
- + echo 2
- 2
- + '[' 2 -eq 3 ']'
- + echo 2
- 2
- + for i in '`seq 1 5`'
- + echo 3
- 3
- + '[' 3 -eq 3 ']'
- + break
- + echo 'job is done'
- job is done
20.14 continue 结束本次循环
忽略 continue 之下的代码, 直接进行下一次循环
continue 命令与 break 命令类似, 只有一点差别, 它不会跳出所有循环, 仅仅跳出当前循环
- [root@aiker02 script]# vim continue.sh
- #!/bin/bash
- for i in `seq 1 5`
- do
- echo $i
- if [ $i -eq 3 ]
- then
- continue
- fi
- echo $i
- done
- echo "job is done"
- [root@aiker02 script]# sh continue.sh
- 1
- 1
- 2
- 2
- 3
- 4
- 4
- 5
- 5
- job is done
20.15 exit 退出整个脚本
- [root@aiker02 script]# cp continue.sh exit.sh
- [root@aiker02 script]# vim !$
- vim exit.sh
- #!/bin/bash
- for i in `seq 1 5`
- do
- echo $i
- if [ $i -eq 3 ]
- then
- exit
- fi
- echo $i
- done
- echo "job is done"
- [root@aiker02 script]# sh exit.sh
- 1
- 1
- 2
- 2
- 3
- [root@aiker02 script]# !vim
- vim exit.sh
- #!/bin/bash
- for i in `seq 1 5`
- do
- echo $i
- if [ $i -eq 3 ]
- then
- exit 2
- fi
- echo $i
- done
- echo "job is done"
- [root@aiker02 script]# sh exit.sh
- 1
- 1
- 2
- 2
- 3
- [root@aiker02 script]# echo $?
- 2
扩展
select 用法 http://www.apelearn.com/bbs/thread-7950-1-1.html
select 也是循环的一种, 它比较适合用在用户选择的情况下.
比如, 我们有一个这样的需求, 运行脚本后, 让用户去选择数字, 选择 1, 会运行 w 命令, 选择 2 运行 top 命令, 选择 3 运行 free 命令, 选择 4 退出. 脚本这样实现:
- #!/bin/bash
- echo "Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit"
- echo
- select command in w top free quit
- do
- case $command in
- w)
- w
- ;;
- top)
- top
- ;;
- free)
- free
- ;;
- quit)
- exit
- ;;
- *)
- echo "Please input a number:(1-4)."
- ;;
- esac
- done
执行结果如下:
- sh select.sh
- Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit
- 1) w
- 2) top
- 3) free
- 4) quit
- #? 1
- 16:03:40 up 32 days, 2:42, 1 user, load average: 0.01, 0.08, 0.08
- USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
- root pts/0 61.135.172.68 15:33 0.00s 0.02s 0.00s sh select.sh
- #? 3
- total used free shared buffers cached
- Mem: 1020328 943736 76592 0 86840 263624
- -/+ buffers/cache: 593272 427056
- Swap: 2097144 44196 2052948
- #?
我们发现, select 会默认把序号对应的命令列出来, 每次输入一个数字, 则会执行相应的命令, 命令执行完后并不会退出脚本. 它还会继续让我们再次输如序号. 序号前面的提示符, 我们也是可以修改的, 利用变量 PS3 即可, 再次修改脚本如下:
- #!/bin/bash
- PS3="Please select a number:"
- echo "Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit"
- echo
- select command in w top free quit
- do
- case $command in
- w)
- w
- ;;
- top)
- top
- ;;
- free)
- free
- ;;
- quit)
- exit
- ;;
- *)
- echo "Please input a number:(1-4)."
- esac
- done
如果想要脚本每次输入一个序号后就自动退出, 则需要再次更改脚本如下:
- #!/bin/bash
- PS3="Please select a number:"
- echo "Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit"
- echo
- select command in w top free quit
- do
- case $command in
- w)
- w;exit
- ;;
- top)
- top;exit
- ;;
- free)
- free;exit
- ;;
- quit)
- exit
- ;;
- *)
- echo "Please input a number:(1-4).";exit
- esac
- done
来源: http://www.bubuko.com/infodetail-2680437.html