1、while
2、for
3、until
4、select
while
#!/bin/bash # 显示一系列数字 count=1 while [ $count -le 6 ]; do echo $count count=$((count + 1)) done echo "finished"
for
输出3次uptime
#!/bin/bash for i in 1 2 3 4 5 do echo "$i-->$(uptime)" done
批量解压缩当前文件夹下所有的.tar.gz 文件
#!/bin/bash for i in `ls ./*.tar.gz` do tar -zxvf $i >/dev/null done
求1~100 的和
#!/bin/bash #注意变量赋值的时候,=两边绝对不能有空格 sum=0 for (( i=1; i<=100; i++ )) do sum=$(( $sum + $i )) done echo "1+2+3+...+100=$sum"
until
#!/bin/bash a=0 until [ $a -gt 10 ] do echo $a a=`expr $a + 1` done
select
#!/bin/bash echo "What is your favourite OS?" select var in "Linux" "Gnu Hurd" "Free BSD" "Other"; do break; done echo "You have selected $var" 该脚本的运行结果如下: What is your favourite OS? 1) Linux 2) Gnu Hurd 3) Free BSD 4) Other #? 1 You have selected Linux