shell的执行流控制
######1.for 循环####################
for ##定义变量
do ##使用变量,执行动作
done ##结束标志
[[email protected] mnt]# vim for.sh
格式一:
[[email protected] mnt]# sh for.sh
格式二:
[[email protected] mnt]# sh for.sh
格式三:
[[email protected] mnt]# sh for.sh
格式四:
[[email protected] mnt]# sh for.sh
#####2.while##############
while true ##条件为真
do ##条件成立所做循环动作
[[email protected] mnt]# vim while.sh
[[email protected] mnt]# sh while.sh 0
#####3.until ##############
until false ##条件为假
do
##条件不成立所做循环动作
done
#####4.if###################
if
then
elif
then
...
else
fi
[[email protected] mnt]# vim if.sh
[[email protected] mnt]# sh -x if.sh
[[email protected] mnt]# sh -x if.sh file
[[email protected] mnt]# sh -x if.sh /mnt/
#####5.case################
[[email protected] mnt]# vim case.sh
[[email protected] mnt]# sh case.sh
[[email protected] mnt]# sh case.sh linux
[[email protected] mnt]# sh case.sh westos
######6.expect#################
问题脚本:
[[email protected] mnt]# vim ask.sh
[[email protected] mnt]# chmod 755 ask.sh
[[email protected] mnt]# /mnt/ask.sh
应答脚本:
[[email protected] mnt]# yum install expect -y
[[email protected] mnt]# vim answer.exp
[[email protected] mnt]# expect answer.exp
[[email protected] mnt]# vim answer.exp
set timeout 1 ##等待时间为一秒
[[email protected] mnt]# vim ask.sh
[[email protected] mnt]# expect answer.exp
改进后:
[[email protected] mnt]# vim answer.exp
[[email protected] mnt]# expect answer.exp
设置expect中的变量:
[[email protected] mnt]# expect answer.exp westos 20 java hard
#####7.break,continue,exit################
break ##终止当前所在语句所有动作进行语句外的其他动作
continue ##终止当此次前循环提前进入下个循环
exit ##脚本退出
continue:
[[email protected] mnt]# vim test.sh
[[email protected] mnt]# sh test.sh
break:
[[email protected] mnt]# vim test.sh
[[email protected] mnt]# sh test.sh
exit:
[[email protected] mnt]# vim test.sh
[[email protected] mnt]# sh test.sh