Linux入门 30_Linux中shell执行流控制语句实例详解

1、for…do循环语句

for语句的作⽤为循环执⾏指定动作,其语句结构如下:

for 	定义变量
do 		使⽤变量,执⾏动作
done 	结束标志

for语句的基本格式有以下四种:
1)in后使用罗列方法定义变量值

示例:

for WESTOS in westos linux lee
do 
	echo $WESTOS
done

2)in后如果变量值连续,可以使用{起始值…结束值}定义变量值

示例:
for WESTOS in {10…1}
do 
	echo $WESTOS
done

3)in后使用seq连续选择命令定义变量值
即(seq 起始值 结束值),这一命令可以设置步长(seq 起始值 步长 结束值)

示例:
for WESTOS in $(seq 1 2 10)
do 
	echo $WESTOS
done

4)使用表达式方式定义变量值
即(单次表达式;条件表达式;末尾循环体)。for循环执行时,会先判断条件表达式是否成立,如果条件成立则执行中间循环体语句,执行完中间循环体后接着执行末尾循环体 ,在执行完末尾循环体后对条件表达式再次进行判断,若条件还成立,则继续重复中间循环体,当条件不成立时则跳出for循环。

示例:
for ((WESTOS=0;WESTOS<10;WESTOS++))
do 
	echo $WESTOS
done

练习:

1、编写脚本check_host.sh批量检测主机网络是否能够ping通
在这里插入图片描述在这里插入图片描述

2、while…do条件语句

while语句作⽤是在条件为真执⾏动作,这一条件语句的结构为:

while ture
do 执⾏动作
done 结束标志

while true ##条件为真
do ##条件成立时做循环动作
done ##结束

实验:

#!/bin/bash
while true
do
  read -p "Please input word: "EXIT
    [ "$EXIT" = "exit" ] && exit
    echo $EXIT
done

在这里插入图片描述

3、until…do条件语句

until语句作⽤是在条件为假执⾏动作,这一条件语句的结构为:

until false 
do 执⾏动作
done 结束标志

until false ##条件为假
do ##条件不成立时做循环动作
done ##结束

实验:

	until [ "$1" = "yes" ]
    do
        echo hello linux
        exit
    done

在这里插入图片描述

4、if …then…elif…then…else…fi 语句

if …then…elif…then…else…fi语句作⽤是多次判定条件执⾏动作, 这一语句可以将多个条件判断结合起来,其代码结构如下:

if				##首次判定
then			##条件成立执行动作
	执⾏动作
elif 			##当首次判定不成立时再次判定
then			##条件成立执行动作
	执⾏动作
'....'			##elif可以多次书写
else			##所有的条件不成立执行动作
	执⾏动作
fi	结束标志 

练习:

要求:
check_file.sh
please input filename:file
file is not exist
file is file
file is direcory
此脚本会一直询问直到用户输入exit为止

while true
do
read -p "please input filename:" FILENAME
if [ "$FILENAME" = "exit" -o "$FILENAME" = "EXIT" ]
then
echo bye
exit
elif [ ! -e "$FILENAME" ]
 then
  echo "文件不存在"
elif [ -d "$FILENAME" ] 
   then
    echo "是个目录"
elif [ -S "$FILENAME" ] 
  then
   echo "是个套结字"
elif [ -L "$FILENAME" ]
   then
   echo "是个软链接"       
elif [ -f "$FILENAME" ] 
 then
 echo "是个普通文件"
elif [ -b "$FILENAME" ] 
 then
 echo "是个块设备"
elif [ -c "$FILENAME" ] 
 then
 echo "是个字符设备"
 fi
 done

5、case语句

#!/bin/bash
case $1 in
    westos) ##如果输入为westos
    echo linux ##输入为linux
    ;;
    linux) ##如果输入为linux
    echo westos ##输出westos
    ;;
    *) ##其余的情况
    echo java 
esac ##结束

在这里插入图片描述

脚本练习:

输入add,添加用户,输入del,删除用户

case $1 in 
   add|ADD)
   read -p "please input username:" a
   id $a > /dev/null &&{
   echo “user is already exist!}||{
   useradd $a
   }
   ;; 
   del|DEL)
   read -p "please input username:" d
   id $d > /dev/null ||{
   echo “user is not exist!}&&{
   userdel $d
   }
   ;;
   *)
   echo error
esac

6.expect(类似于shell,命令解释器)

expect看的是命令,而不是行数

脚本练习:

vim ask.sh
#!/bin/bash
read -p "Please input your name: " NAME
read -p "Please input your age: " AGE
read -p "Wich objective you study: " OBJ
read -p "Are you happy? " FEEL
echo "$NAME is $AGE's old study $OBJ FEEL $FEEL"

expect应答脚本:

   dnf install expect -y

1.ask.sh(问题脚本)

#!/bin/bash
read -p "Please input your name: " NAME
read -p "Please input your age: " AGE
read -p "Wich objective you study: " OBJ
read -p "Are you happy? " FEEL
echo "$NAME is $AGE's old study $OBJ FEEL $FEEL"

2.answer.exp(回答脚本)

  #!/usr/bin/expect
    set timeout 1
    set NAME [ lindex $argv 0 ]
    set AGE  [ lindex $argv 1 ]
    set OBJ  [ lindex $argv 2 ]
    set FEEL [ lindex $argv 3 ]
    spawn /mnt/ask.sh
    expect {
            "name"   { send "$NAME\r";exp_continue }
            "age"    { send "$AGE\r"; exp_continue }
            "objective" { send "$OBJ\r"; exp_continue }
            "happy"        { send "$FEEL\r" }
            }
            expect eof

shell应答脚本:

sh answer.exp lee 18 linux happy
#!/bin/bash
 
/usr/bin/expect <<EOF
set timeout 3
spawn /mnt/ask.sh
expect {
    "name" { send "$1\r";exp_continue }
    "age"  { send "$2\r";exp_continue }
    "objective" { send "$3\r";exp_continue }
    "happy" { send "$4\r" }
}
expect eof
EOF

7.break,continue,exit

continue 	##终止当此次前循环提前进入下个循环
break    	##终止当前所在语句所有动作进行语句外的其他动作
exit     	##脚本退出
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿王不想秃头

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值