执行流语句
执行流就是在脚本中记录的命令执行的顺序,默认的脚本执行顺序是从上到下依次执行
1.for循环语句
for语句的作用为循环执行指定动作,语句结构如下:
for # 定义变量
do # 对变量的动作
done # 结束标志
(1) for语句使用罗列方法定义变量
for WESTOS in westos linux redhat
do echo $WESTOS
done

(2)in后跟连续变量
for WESTOS in {1..10}
do echo $WESTOS
done

(3)in后使用seq连续选择命令定义变量
seq 1 10 将会顺序显示1-10数字
seq 1 2 10 意思是1-10之间每间隔2取一次值,则会显示1 3 5 7 9
即
for WESTOS in $(seq 1 2 10)
do echo $WESTOS
done

实验1
for语句写脚本批量测试同网段主机(30-40):
#!/bin/bash
for ens in 172.25.254.{40..50}
do
ping -c1 -w1 $ens &> /dev/null && {
echo $ens is up
}||{
echo $ens is down
}
done
效果如图:所有测试结果会依次显示出来
这里只有一台主机40开着,所以只有一台检测为up

测试主机ping通不通的时候,必须要加-c表示次数-w表示时间,否则指令不结束,脚本不出结果
脚本练习:
vim user_list 写一个用户列表文件,如:user1 user2 user3 sh user.sh user_list
执行脚本时,$1为你的用户列表文件 要求:
文件不存在时报错,文件存在则检测文件中指定用户列表中的用户是否存在,存在显示存在,不存在直接建立用户
#!/bin/bash
[ -z "$1" ] && {
echo "Errow :Please input filename fllowing $0: "
exit
}
[ ! -e "$1" ] && {
echo "Errow: $1 is not exist !!"
exit
}
for user in `cat $1`
do
id $user &> /dev/null && {
echo $user is exist
}||{
useradd $file
}
done
如图:

2.while条件语句
while语句作用是在条件为真时执行动作,语句结构为:
while true # 条件为真
do # 执行动作
done # 结束标志
例如:
效果如图:

3.until条件语句
until语句作用是在条件为假执行动作,这一条件语句的结构为:
until false
do
done
例如:
until条件就是直到达到此条件,否则一直执行do

4.if条件判断语句
if语句是多次判定条件执行动作,这一语句可以将多个条件判断结合起来,代码结构如下:
if
then
#执行动作
elif
then
#执行动作
else
#执行动作
fi #结束标志
例如:
效果如图:

脚本练习:
用执行流语句写一个循环检测文件类型的脚本
要求:运行脚本提示用户输入文件名,文件名不能为空,当文件不存在时需要有提示,当文件存在时自动检测文件类型并输出
#!/bin/bash
while true
do
read -p "Please input filename :" filename
if [ "$filename" = "exit" ]
then
echo bye
exit
elif [ -z "$filename" ]
then
echo "Errow: Please input filename following script !!"
elif [ ! -e "$filename" ]
then
echo "$filename is not exist "
elif [ -d "$filename" ]
then
echo "$filename is directory"
fi
done
效果如图:

5.case应答语句
if语句是判断机制,从第一个条件开始判断直到满足某个条件为止,这种判断机制效率较低
case语句是点名机制,会横向对比所掌握的所有条件元素,对号入座,语句效率较高
case语句结构为:
case $1 in
word|WORD)
action1
;;
word2|WORD2)
action2
;;
*)
action3
esac #结束标志

效果:

通过sh -x case.sh westos 可以看到脚本运行过程,case语句检测输入的字符时只判断一次对号入座,相比较if语句更加高效
脚本练习:
编写对用户进行管理的脚本ctrl_user.sh 脚本要求:
执行脚本时输出提示:提示用户选择需要执行的动作useradd或者userdel,输入其他动作指>令会报错,此环节也可以输入exit退出
选择useradd后提示输入用户名,可以检测到用户是否存在,如果存在提示已存在,如果不存在提示输入新建用户密码,显示创建成功!接着继续提示输入用户名,继续出创建用户,直>到输入exit退出
选择userdel后提示输入用户名,可以检测该用户是否存在,如果存在直接删除并显示删除成
功,如果不存在直接提示该用户不存在无法删除,删除成功后继续提示输入用户名,直到输>入exit退出
#!/bin/bash
while true
do
read -p "Please input action:" action
case $action in
useradd|a)
while true
do
read -p "Please input username:" username
[ "$username" = "exit" ] && break
id $username &> /dev/null && {
echo "$username is exist !"
}||{
useradd $username &> /dev/null
read -p "Please input password for user: " -s PASS
echo $PASS | passwd --stdin $username &> /dev/null
echo "$username is created !"
}
done
;;
delete|d)
while true
do
read -p "Please input username:" username
[ "$username" = "exit" ] && break
id $username &> /dev/null && {
userdel -r $username &> /dev/null &&
echo "$username is deleted"
}||{
echo "$username is not exist"
}
done
;;
exit)
echo bye
exit
;;
*)
echo wrong action !!!
;;
esac
done
效果如图:

6.终止条件continue/break/exit
continue 作用:终止此次循环提前进入下一个循环
break 作用:终止当前所在语句块的所有动作进行语句外的其他动作
exit 作用:退出脚本
实验:
vim test.sh 测试三个终止条件的效果
#!/bin/bash
for N in {1..10}
do
if [ "$N" = "4" ]
then
echo luck number!!
#continue
#break
exit 66
fi
echo $N
done
echo westos linux
$?表示退出值,退出值默认为0,$?为0时表示命令无任何报错,不为0时表示有相应的报错
echo $? 显示退出值;退出值范围0~255

7.expect交互应答语句
dnf install expect -y 安装expect语句安装包
(1)编写问题脚本:
运行脚本时逐个输出提示语,提示用户输入姓名、年龄和学科、心情
#!/bin/bash
read -p "what's your name:" NAME
read -p "How old are you:" AGE
read -p "Which subject you staudy:" OBJ
read -p "Are you happy?" FEEL
echo $NAME is $AGE\'s old study $OBJ feel $FEEL
(2)应答脚本:
#!/usr/bin/expect
set timeout 1
set NAME [ lindex $argv 0 ]
set AGE [ lindex $argv 1 ]
set OBJ [ lindex $argv 2 ]
set FELL [ 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 }
"fell" { send "$FELL\r" }
}
expect
脚本练习:
编写交互脚本host_list.sh检测172.25.254.0网段网络是否开启,开启显示主机名,未开启>显示down
#!/bin/bash
Auto_ssh()
{
/usr/bin/expect <<EOF
spawn ssh -l root $1 hostname
expect {
"yes/no" { send "yes\r";exp continue }
"password:" {send "westos\r"}
}
expect eof
EOF
}
[ -z "$1" ] && {
echo "Please input check ipaddress !!"
exit
}
ping -c1 -w1 $1 &> /dev/null && {
Auto_ssh $1 | tail -n 1
}||{
echo $1 is down
}
效果如图:

本文详细讲解了Linux中的执行流语句,包括for循环、while条件语句、until条件语句、if条件判断、case应答语句、终止条件(continue/break/exit)以及expect交互应答语句。通过实例和脚本练习,阐述了这些语句的使用方法和应用场景,旨在帮助读者深入理解Linux shell编程。
2517

被折叠的 条评论
为什么被折叠?



