shell的执行流控制for/while/until/if/case

1.for语句

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

格式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=0;WESTOS<10;WESTOS++))
do 
	echo $WESTOS
done

脚本练习1:
check_host.sh
用此脚本检测当前10台主机与您当前主机直连主机是否网络通畅
如果通畅显示ip

#!/bin/bash
for ((IP=1;IP<11;IP++))
do
    ping -c1 -w1 172.25.254.$IP &>/dev/null && echo 172.25.254.$IP
done

在这里插入图片描述
脚本练习2:
在这里插入图片描述

create_user.sh userfile passfile

#!/bin/bash
Line_Num=`sed -n '$=' userfile`
for LINE in `seq 1 $Line_Num`
do
    USERNAME=`sed -n ${LINE}p $1`
    PASSWORD=`sed -n ${LINE}p $2`
    id $USERNAME &> /dev/null &&{
        echo $USERNAME is exist!
    }||{
        useradd $USERNAME
        echo $PASSWORD |passwd --stdin $USERNAME &> /dev/null && echo $USERNAME is created
    }
done

在这里插入图片描述

2.while

while true #条件为真
do #条件成立所循环的动作
done

3.until

until false #条件为假
do
#条件不成立所作循环动作
done

4.if

if
then
elif

在这里插入图片描述

#!/bin/bash
until false
do
    read -p "please input word: " WORD

if [ "$WORD"  =  "exit" ]
then
    echo bye
    exit
elif [ ! -e "$WORD" ]
then
    echo not exited
elif [ -d "$WORD" ]
then
    echo is a dir
elif [ -f "$WORD" ]
then
    echo is a file

fi
done

5.case

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

在这里插入图片描述

#!/bin/bash
read -p "please choose disk/memory/upload: " WORD
case $WORD in
    disk)
    watch -n 1 "df -h"
    ;;
    memory)
    watch -n 1 "free -h"
    ;;
    upload)
    top
    ;;
    *)
    echo "error words
esac

6. except

6.1问题脚本

vim ask.sh

#!/bin/bash
read -p "what's your name; " NAME
read -p "How old are you: " AGE
read -p "Which class: " CLASS
read -p "Are you appy: " FEEL
echo $NAME is $AGE\'s old study $CLASS feel $FEEL

6.2应答脚本

vim answer.exp

#!/usr/bin/expect
set timeout 2
spawn /mnt/ask.sh
expect "name"
send "lee\r"
expect "old"
send "18\r"
expect "class"
send "linux\r"
expect "happy"
send "happy\r"
expect eof

chmod +x ask.sh

在这里插入图片描述

此处用EOF导向ask.sh也可以实现:
在这里插入图片描述

在expect应答脚本中加入非交互输入

#!/usr/bin/expect
set NAME [ lindex $argv 0 ]
set AGE [ lindex $argv 1 ]
set CLASS [ lindex $argv 2 ]
set FEEL [ lindex $argv 3 ]

set timeout 1
spawn /mnt/ask.sh
expect {
        "name" { send "$NAME\r" ; exp_continue }
        "old" { send "$AGE\r" ; exp_continue }
        "class" { send "$CLASS\r" ; exp_continue }
        "happy" { send "$FEEL\r" }
}
expect eof

在这里插入图片描述
在shell中加入expect:

#!/bin/bash
echo hello westos
/usr/bin/expect << EOF
spawn /mnt/ask.sh
expect {
"name" { send "$1\r" ; exp_continue }
"old" { send "$2\r" ; exp_continue }
"class" { send "$3\r" ; exp_continue}
"happy" { send "$4\r" }
}
expect eof
EOF

在这里插入图片描述
练习脚本:
在这里插入图片描述

#!/bin/bash
AUTO_SSH()
{
/usr/bin/expect <<EOF
spawn ssh root@$1 hostname
expect {
"yes/no" { send "yes\r" ; exp_continue }
"password:" { send "$2\r" }
}
expect eof
EOF
}

for Host_Num in {1..10}
do
    ping -c1 -w1 172.25.254.$Host_Num &> /dev/null
    if  [ "$?" = "0" ]
    then
        Host_Name=`AUTO_SSH 172.25.254.$Host_Num westos | tail -n 1`
        grep $Host_Name /mnt/hosts_list &> /dev/null || {
            echo "172.25.254.$Host_Num $Host_Name" >> /mnt/hosts_list
        }
    fi

done

sed 's/^M//g' -i /mnt/hosts_list

在这里插入图片描述

7.break,continue,exit

continue终止当前循环直接进入下个循环
break终止当前所在语句所有动作进行语句外的其他动作
exit脚本退出
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值