linux学习笔记-Shell(五):shell中的执行流控制

一.for语句

作⽤
为循环执⾏动作
for语句结构
for 定义变量
do 使⽤变量,执⾏动作
done 结束标志

for语句的基本格式

格式1:

#!/bin/bash
for WESTOS in $(seq 1 2 10)
do
 echo $WESTOS
done

在这里插入图片描述

格式2:

for WESTOS in westos linux lee
do
 echo $WESTOS
done

格式3:

for WESTOS in {10..1}
do
 echo $WESTOS
done

格式4:

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

脚本练习

check_host.sh
⽤此脚本检测10台与您当前主机直连主机是否⽹络通常
如果⽹络通常请显⽰主机的ip列表

for IP in 172.25.254.$(seq 1 10)
do
ping 172.25.254.$IP &> /dev/null && {
      echo   172.25.254.$IP
}
done

二.条件语句

while…do语句

作⽤:
条件为真执⾏动作

语句结构

while ture
do
done

until…do 语句

作⽤:

条件为假执⾏动作

语句结构

until false
do
done

if语句

if…then…elif…then…else…fi

作⽤:
多次判定条件执⾏动作

代码结构

if
then
elif
then
... 
else
fi

脚本练习

1.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 :" FILE
        if  [ $FILE = "exit" ]
        then
            echo exit
             exit
        elif [ ! -e "$FILE" ]
        then
            echo  $FILE is not exit
        elif  [ -f "$FILE" ]
        then
            echo $FILE is file
        elif  [ -d "$FILE" ]
        then
            echo $FILE is direcory
        fi
done

2.如图
在这里插入图片描述

DEL()
{     read -p "please input username :" USERNAME
      [ "$USERNAME" = "exit" ]  &&  {
        MAN
       }
      id $USERNAME &> /dev/null && {
        userdel -r $USERNAME &> /dev/null  && {
        echo $USERNAME is deleted 
    } 
  } || {
        echo $USERNAME is not exist 	  
        DEL
  }
}

ADD()
{
     read -p "please input username :" USERNAME
      [ "$USERNAME" = "exit" ]  &&  {
         MAN
       }
     id  $USERNAME &> /dev/null && {
         echo  $USERNAME is exist 
         ADD
     } || {
         read -p "please input passwd :" PASSWD
         useradd $USERNAME
         echo $PASSWD |  passwd --stdin $USERNAME &> /dev/null  && {
	 echo  $USERNAME is created 
	 }
	 

     }
}

MAN()
{
while true
do	
    read -p "please input Action : del|add|exit :" ACTION
    case $ACTION in
	exit)
	echo  Action : exit
	exit
	;;
        del)
	DEL
	;;
        add)
	ADD
	;;
        *)
	echo wrong Action
    esac
done
}
MAN

三.case

语句基本结构

case $1 in
word1|WORD1)
action1
;;
word2|WORD2)action2
;;
*)
action3
esac

四.expect

问题脚本

#!/bin/bash
read -p "what's your name:" NAME
read -p "How old are you: " AGE 
read -p "Which objective: " OBJ
read -p "Are you ok? " OK
echo $NAME is $AGE's old study $OBJ feel $OK

应答脚本

#!/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 }
"old" { send "$AGE\r";exp_continue }
"objective" { send "$OBJ\r";exp_continue }
"ok" { send "$FEEL\r" }
}
expect eof

在这里插入图片描述
在这里插入图片描述

脚本练习

host_list.sh
检测172.25.254.1-172.25.254.10⽹络是否开启
如果⽹络正常请⽣成解析列表hosts_list
格式如下
ip 主机名称
例如:172.25.254.1为开启状态主机名为westos_student1.westos.org
hosts_list中
172.25.254.1 westos_student1.westos.org
在这里插入图片描述

五.break,continue,exit

contiue      终⽌当此次前循环提前进⼊下个循环
break        终⽌当前所在语句所有动作进⾏语句外的其他动作
exit         脚本退出

完整循环输出:
在这里插入图片描述
在这里插入图片描述
exit:
在这里插入图片描述

在这里插入图片描述
break:
在这里插入图片描述
continue:
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值