Case statement
case $变量名 in "值1") ? 如果变量的值等于值1,则执行程序1 ;; ?"值2") ? 如果变量的值等于值2,则执行程序2 ;; ? ...省略其他分支... ? *) ? 如果变量的值都不是以上的值,则执行此程序 ;;esac ?//case反过来写
"case语句的使用实例"#!/bin/bashread "please input yes /no": chocase "$cho" in "yes") ? ? echo "输入正确!";; ?"no") ? ? echo "输入错误!";; ? ?*) ? ? echo "输入异常!";;esac------------------------------------使用case的语句进行流程控制------------[[email protected] shell]# cat case_a.sh #!/bin/bashread opcase $op in ?a) ? ?echo "你输出的信息为 a ";; ?b)
For statement
for 变量 in 值1 值2 值3 ... do ?程序 done
-----------使用实例--------- #! /bin/bash for i in 1 2 3 4 5 ?do ? ?echo $i ?done-----------使用实例-------------解压缩某个目录下的压缩文件#!/bin/bashcd /root/test/ls *.tar.gz > ls.logls *.tgz >> ls.logfor i in $(cat ls.log) ?do ? ?tar -zxf &> /dev/null ?donerm -rf ls.log
Select loop
select 变量 in 列表 ?do ? ?cmd ... ?done
[[email protected] shell]# vim g.sh#!/bin/bashselect a in "a" "b" "c" "d"do echo $a breakdone[[email protected] shell]# sh g.sh 1) a2) b3) c4) d#? 3 //选择第三行信息c
Until cycle
until循环,和while循环相反,until循环时只要条件判断式不成了则进行循环并执行循环程序。一旦循环条件成立,则终止循环?until [ 条件判断式 ] do ?程序 done
-----------使用实例------------#!/bin/bash#从1加到100i=1s=0until [$i -ge 100] #如果变量i的值大于等于100,则停止执行循环 do ?s=$(($s+$i)) ?i=$(($i+1)) doneecho "the sum is : $s"
While loop
------------------使用实例--------------#!/bin/bashi=1sum=0while [ $i -le 100 ] do ?sum=`expr $sum + $i` ?i=`expr $i + 1` doneecho "the sum is : $sum" ? ? [[email protected] shell]# sh while.sh the sum is : 5050 ? ? ? ? ? ? ? ?[[email protected] shell]# cat while.sh #!/bin/bashi=0while [ $i -le 100 ] do ?i=`expr $i + 1 ` ?if [ $i -eq 10 ] ? ?then ? ? ?echo "contine"
Shell Process Control Statements