shell的执行流控制

本文详细介绍了Shell脚本中的循环控制结构,包括for、while、until、if、case循环,以及break、continue和exit的使用。通过实例脚本演示了如何在不同场景下运用这些控制流,如check_host.sh用于检查网络连接,system_watch.sh实现系统监控。
摘要由CSDN通过智能技术生成


一、for 循环
for	#定义变量
do	#使用变量,执行动作
done	#结束标志

格式1:

#!/bin/bash
for WESTOS in `seq 2 2 10`
do
        echo $WESTOS
done

格式2:

for WESTOS in 1 2 3
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列表。

#!/bin/env bash
for PC in `seq 1 10`
do
    ping -w 1 -c 1 172.25.254.$PC &> /dev/null && 
	echo 172.25.254.$PC
done
二、while循环
while ture	#条件为真
do		#条件成立所作循环动作
done
三、until 循环
until false	##条件为假
do
		#条件不成立所作循环动作
done
四、if 循环
if
then
elif
then
...
else
fi

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

#!/bin/env bash
IF()
{
if [ -z "$FILE" ]
then       
	echo "未指定检测文件,请指定!"
	READ
elif [ ! -e "$FILE" ] 
then
	echo "此文件不存在!"
	READ
elif [ -d "$FILE" ]
then
	echo "目录"
	READ
elif [ -S "$FILE" ]
then
	echo "套接字"
	READ
elif [ -L "$FILE" ]
then
	echo "软链接"
	READ
elif [ -f "$FILE" ]
then
	echo "普通文件"
	READ
elif [ -b "$FILE" ]
then
	echo "块设备"
	READ
elif [ -c "$FILE" ]
then
	echo "字符设备"
	READ
fi
}
READ()
{
        read -p "please input filename: " FILE
	[ "$FILE" = "exit" ]||[ "$FILE" = "EXIT" ]&&{
	echo bye!
        exit
	}||{
        IF
	}
}
READ
五、case 循环
case $1 in
	word1|WORD1)
	action1
	;;
	word2|WORD2)
	action2
	;;
	*)
	action3
esac

脚本练习:system_watch.sh disk memory upload (每秒显示)

disk 监控磁盘使用情况
memory 监控内存使用情况
upload 监控启动负载

#!/bin/env bash
case $1 in
	disk)
	watch -n 1 df -H
	;;
	memory)
	watch -n 1 free -m
	;;
	upload)
	watch -n 1 w
	;;
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网络是否开启,如果网络正常请生成解析列表host_list.
格式如下:
ip 主机名称
例如:172.25.254.1为开启状态,主机名为westos_student1.westos.org
host_list中
172.25.254.1   westos_student1.westos.org
#!/bin/env bash
Host_Name()
{
/usr/bin/expect <<EOF
spawn ssh root@$1 hostname
expect {
"yes" { send "yes\r" ; exp_continue }
"password" { send "$2\r"  }
}
expect eof
EOF
}
for IP in {1..10}
do
        ping -w1 -c1 172.25.254.$IP &> /dev/null
	if [ "$?" = "0" ]
	then
		NAME=`Host_Name 172.25.254.$IP westos | tail -n 1`
		grep $NAME /mnt/host_list &> /dev/null || {
                echo "172.25.254.$IP $NAME" >> /mnt/host_list
	}
	fi
done
sed 's/
//g' -i /mnt/host_list
七、break, continue, exit
contiue		##终止当此次前循环提前进入下个循环
break		##终止当前所在语句所有动作进行语句外的其他动作
exit		##脚本退出
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值