shell的执行流控制

######1.for  循环####################

for          ##定义变量

do          ##使用变量,执行动作

done      ##结束标志

[[email protected] mnt]# vim for.sh

格式一:

shell的执行流控制

[[email protected] mnt]# sh for.sh

shell的执行流控制

格式二:

shell的执行流控制

[[email protected] mnt]# sh for.sh

shell的执行流控制

格式三:

shell的执行流控制

[[email protected] mnt]# sh for.sh

shell的执行流控制

格式四:

shell的执行流控制

[[email protected] mnt]# sh for.sh

shell的执行流控制

#####2.while##############

while true     ##条件为真

do                ##条件成立所做循环动作

[[email protected] mnt]# vim while.sh

shell的执行流控制

[[email protected] mnt]# sh while.sh 0

shell的执行流控制

#####3.until ##############

until false    ##条件为假

do

                    ##条件不成立所做循环动作

done

#####4.if###################

if

then

elif

then

...

else

fi

[[email protected] mnt]# vim if.sh

shell的执行流控制

[[email protected] mnt]# sh -x if.sh

[[email protected] mnt]# sh -x if.sh file

[[email protected] mnt]# sh -x if.sh /mnt/

shell的执行流控制

#####5.case################

[[email protected] mnt]# vim case.sh

shell的执行流控制

[[email protected] mnt]# sh case.sh

[[email protected] mnt]# sh case.sh linux

[[email protected] mnt]# sh case.sh westos

shell的执行流控制

######6.expect#################

问题脚本:

[[email protected] mnt]# vim ask.sh

shell的执行流控制

[[email protected] mnt]# chmod 755 ask.sh

[[email protected] mnt]# /mnt/ask.sh

shell的执行流控制

应答脚本:

[[email protected] mnt]# yum install expect -y

[[email protected] mnt]# vim answer.exp

shell的执行流控制

[[email protected] mnt]# expect answer.exp

shell的执行流控制

[[email protected] mnt]# vim answer.exp

set timeout 1     ##等待时间为一秒

shell的执行流控制

[[email protected] mnt]# vim ask.sh

shell的执行流控制

[[email protected] mnt]# expect answer.exp

shell的执行流控制

改进后:

[[email protected] mnt]# vim answer.exp

shell的执行流控制

[[email protected] mnt]# expect answer.exp

shell的执行流控制

设置expect中的变量:

shell的执行流控制

[[email protected] mnt]# expect answer.exp westos 20 java hard

shell的执行流控制

#####7.break,continue,exit################

break            ##终止当前所在语句所有动作进行语句外的其他动作

continue       ##终止当此次前循环提前进入下个循环

exit               ##脚本退出

continue:

[[email protected] mnt]# vim test.sh

shell的执行流控制

[[email protected] mnt]# sh test.sh

shell的执行流控制

break:

[[email protected] mnt]# vim test.sh

shell的执行流控制

[[email protected] mnt]# sh test.sh

shell的执行流控制

exit:

[[email protected] mnt]# vim test.sh

shell的执行流控制

[[email protected] mnt]# sh test.sh

shell的执行流控制