Shell--条件循环语句

一.for语句
1.for语句的基础定义
for语句通过对变量的定义取值举鼎循环的执行次数,根据变量赋值的次数,for循环内的指令将被执行相同的次数
语法格式1
for 变量 in 值1、2、3…n
do
命令序列
done
语法格式2
for ((初始化变化值;结束循环条件;运算))
do
命令序列
done
2.for语句的示例
i=5时,输出hello,结束时输出end

[root@server mnt]# vim test.sh 
[root@server mnt]# cat test.sh
#!/bin/bash
for i in {1..10}
do
          [ "$i" = "5" ]&&{
                  echo hello
          }
          echo $i
done
echo end
[root@server mnt]# sh test.sh
1
2
3
4
hello
5
6
7
8
9
10
end

i=5时输出hello并结束脚本的执行

[root@server mnt]# vim test.sh
[root@server mnt]# cat test.sh
#!/bin/bash
for i in {1..10}
do
          [ "$i" = "5" ]&&{
                  echo hello
                  exit
          }
          echo $i
done
echo end
[root@server mnt]# sh test.sh
1
2
3
4
hello

i=5时输出hello,跳出本次循环,继续执行下一次循环,结束后输出end

[root@server mnt]# vim test.sh
[root@server mnt]# cat test.sh
#!/bin/bash
for i in {1..10}
do
          [ "$i" = "5" ]&&{
                  echo hello
                  continue
          }
          echo $i
done
echo end
[root@server mnt]# sh test.sh
1
2
3
4
hello
6
7
8
9
10
end

i=5时输出hello并且跳出所在的循环,继续执行脚本

[root@localhost mnt]# vim file.sh
[root@localhost mnt]# cat file.sh
#!/bin/bash
for i in {1..10}
do
       [ "$i" = "5" ]&&{
             echo hello    
             break
        }
        echo $i
done
[root@localhost mnt]# sh file.sh
1
2
3
4
hello

exit、break、continue的区别
exit ##直接结束脚本的执行
break ##跳出所在的循环语句并且继续执行脚本
continue ##结束本次循环继续下一次的循环
seq 指定步长为2,由2开始10结束,每隔2个数字进行输出 输出偶数

[root@server mnt]# vim file.sh
[root@server mnt]# cat file.sh
#!/bin/bash
for i in `seq 2 2 10`
do
          echo $i
done          
[root@server mnt]# sh file.sh
2
4
6
8
10

以1开始以10 结尾每隔2个数字进行输出 输出奇数

[root@server mnt]# vim file.sh
[root@server mnt]# cat file.sh
#!/bin/bash
for i in `seq 1 2 10`
do
          echo $i
done          
[root@server mnt]# sh file.sh
1
3
5
7
9

3.for语句的应用示例
脚本要求:用户存在输出用户存在,用户不存在创建用户,并且用户密码取决于用户密码文件
[root@localhost mnt]# vim user_create.sh
在这里插入图片描述
在这里插入图片描述
运行结果展示

[root@localhost mnt]# sh user_create.sh
Error:Please input userfile and passwdfile!!
[root@localhost mnt]# sh user_create.sh userfile passwdfile
westos1 is created successfully!!
westos2 is created successfully!!
westos3 is created successfully!!
[root@localhost mnt]# sh user_create.sh userfile passwdfile
westos1 is exist!!
westos2 is exist!!
westos3 is exist!!

二.while语句
1.定义:用于循环一系列的循环语句
2.while语句的格式
while 条件
do 动作
done 结束标志
3.while语句的应用
文件不存在则输出文件不存在并结束循环 文件名为exit时退出脚本的执行

[root@localhost mnt]# vim test.sh
[root@localhost mnt]# cat test.sh
#!/bin/bash
while true
do
          read -p "Please input a filename:" FNAME
          if
                   [ "$FNAME" = "exit" ]
          then
                   exit
          elif
                   [ ! -e  "$FNAME" ]
          then
                    echo $FNAME is not exist!!
          fi
done
[root@localhost mnt]# sh test.sh
Please input a filename:haha
haha is not exist!!
Please input a filename:exit

以while 语句判断文件的类型

[root@localhost mnt]# vim type_file.sh

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

[root@localhost mnt]# sh type_file.sh
Please input the filename:/etc
/etc is a directory!!
Please input the filename:/etc/passwd
/etc/passwd is a common file!!
Please input the filename:/bin
/bin is a link!!
Please input the filename:/dev/vdb
/dev/vdb is a block device!!
Please input the filename:exit

三.until语句
1.until基础知识
until 循环执行一系列命令直至条件为 true 时停止
until 循环与 while 循环在处理方式上刚好相反
一般 while 循环优于 until 循环,极少数情况下,until 循环更加有用
2.until语句的格式
until 条件
do 动作
done 结束标志
3.until语句的应用

[root@localhost mnt]# vim luck.sh
[root@localhost mnt]# cat luck.sh  ##直到i小于10的时候循环语句不再执行
#!/bin/bash
i=15
until [ "$i" -lt "10" ]
do
	echo $i
	((i--))
done
[root@localhost mnt]# sh luck.sh
15
14
13
12
11
10

四.case语句
1.case 语句基础知识
case语句为多选择语句,可以用case语句匹配一个值与一个模式,如果匹配成功,执行相匹配的命令。
case 语句在进行判断的时候是并发进行的,处理效率较高,if 语句在进行条件判断的时候是逐条从上往下,处理效率较低
2.case 语句的格式
case
word1)
action
;;
word 2)
action
;;
*)
action_last
esac
3.case语句的应用示例
脚本要求:当执行创建用户动作时,用户存在则输出用户存在,用户不存在则创建用户;当执行删除用户动作时,用户存在则删除用户,用户不存在则输出用户不存在

[root@localhost mnt]# vim action_user.sh

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

[root@localhost mnt]# sh action_user.sh
[C]reate [D]elete
Please input your action:c
Please input a username:westos
Please input passwordwestos
westos is created successfully!!
[root@localhost mnt]# sh action_user.sh
[C]reate [D]elete
Please input your action:c
Please input a username:westos
westos is exist!!
[root@localhost mnt]# sh action_user.sh
[C]reate [D]elete
Please input your action:d
Please input a username:westos
westos is deleted successfully!!
[root@localhost mnt]# sh action_user.sh
[C]reate [D]elete
Please input your action:d
Please input a username:westos
westos is not exist!!

五.expect语句
1.expect语句的简介
expect 相对于语句,更像是一条命令
expect 是自动应答命令用于交互式命令的自动执行
spawn 是expect中的监控程序,其运行后会监控命令提出的交互问题
send 发送问题答案给交互命令
exp_continue 表示当问题不存在时继续回答下面的问题
expecte of 表示问题回答完毕退出expect环境
interact 表示问题回答完毕留在交互界面
set NAME [ lindex $argvn ] 定义变量
2.expect语句的应用
(1)回答问题
安装expect软件

[root@localhost mnt]# yum install expect.x86_64 -y

交互式脚本编辑

[root@localhost mnt]# vim answer.sh
[root@localhost mnt]# cat answer.sh
#!/bin/bash
read -p "who are you?" NAME
read -p "how old are you?" AGE
read -p "where are you from?" ADDR
read -p "what are you doing now?" ACTION
echo $NAME is $AGE yeas old from $ADDR,now $ACTION

脚本运行时是一步步输入所需要的参数

[root@localhost mnt]# sh answer.sh
who are you?lee   
how old are you?18
where are you from?shanxi
what are you doing now?reading book
lee is 18 yeas old from shanxi,now reading book

采用expect脚本实现此目标

[root@localhost mnt]# vim answer.exp
[root@localhost mnt]# cat answer.exp
#!/usr/bin/expect  ##注意运行环境
set NAME [ lindex $argv 0 ]
set AGE [ lindex $argv 1 ]
set ADDR [ lindex $argv 2 ]
set ACTION [ lindex $argv 3 ]  ##定义4个变量
spawn /mnt/answer.sh  ##监控需要进行交互式的脚本
expect {
	"who" { send "$NAME\r";exp_continue }
	"how" { send "$AGE\r";exp_continue }  ##exp_continue:当问题步村在时继续回答下面的问题
	"where" { send "$ADDR\r";exp_continue }
	"what" { send "$ACTION\r";} 
}
expect eof  ##问题回答完毕,退出expect环境

执行expect脚本

[root@localhost mnt]# chmod 777 answer.sh  ##给交互式脚本可执行权限
[root@localhost mnt]# expect answer.exp lee 18 shanxi reading
spawn /mnt/answer.sh
who are you?lee
how old are you?18
where are you from?shanxi
what are you doing now?reading
lee is 18 yeas old from shanxi,now reading

采用将expect语句嵌入sh脚本中进行运行

[root@localhost mnt]# vim question.sh
[root@localhost mnt]# cat question.sh
#!/bin/bash
/usr/bin/expect <<EOF
spawn /mnt/answer.sh
expect {
"who"  { send "$1\r";exp_continue }
"how" { send "$2\r";exp_continue }
"where" { send "$3\r";exp_continue }
"what" { send "$4\r";}
}
expect eof
EOF

执行脚本

[root@localhost mnt]# sh question.sh lee 18 shanxi reading
spawn /mnt/answer.sh
who are you?lee
how old are you?18
where are you from?shanxi
what are you doing now?reading
lee is 18 yeas old from shanxi,now reading

(2)对教室第30-40台主机进行连接,如果连接成功则输出此主机用户名

[root@localhost mnt]# vim hostname.sh
[root@localhost mnt]# vim hostname.sh

在这里插入图片描述
输出结果

[root@localhost mnt]# sh hostname.sh
[root@localhost mnt]# cat host
172.25.254.31 foundation31.ilt.example.com
172.25.254.33 foundation33.ilt.example.com
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值