shell脚本中的结构化命令(if-then-else、case、for、while、until) && 脚本中的循环控制

本文详细介绍了Shell脚本中的结构化命令,包括if-then、if-then-else、case、for、while和until语句的使用,并给出了相关示例。同时,讲解了条件测试、循环控制中的break和continue命令,以及如何在脚本中进行有效的逻辑控制。
摘要由CSDN通过智能技术生成

1、 结构化命令
上一次我们学习了shell脚本的一些基础知识,包括环境变量、重定向、数学运算、退出脚本的方式等,想了解的可以戳这个: shell脚本基础
之前,在我们的示例shell脚本里,shell按照命令在脚本中出现的顺序依次进行处理。然而有时候,我们需要对shell脚本中的命令施加一些逻辑流程控制。有一类命令会根据条件使脚本跳过某些命令。这样的命令通常称为结构化命令,它允许我们改变程序执行的顺序。下面,我们学习一下常见的结构化命令 :
2、if-then语句
一般格式如下 :

if command
then 
	commands
fi

bash shell的if语句会运行if后面的那个命令。如果该命令的退出状态码是0即该命令成功运行,位于then部分的命令就会被执行。如果该命令的退出状态码是其他值,then部分的命令就不会被执行,bash shell会继续执行脚本中的下一个命令。fi语句用来表示if-then语句到此结束。
示例 : 判断用户名当前是否在系统上使用,如果有用户使用了那个登录名,脚本会显示一些文本信息并列出该用户HOME目录的bash文件。

[root@relay3.mobvista.com:101.251.254.6 shell]#cat test.sh
#!/bin/bash
# testing if-then
username=Miya
if grep $username /etc/passwd
then
        echo "$username exits!"
        ls -a /home/$username/.b*
fi

[root@relay3.mobvista.com:101.251.254.6 shell]#sh test.sh
Miya:x:1026:1026::/home/Miya:/bin/bash
Miya exits!
/home/Miya/.bash_logout  /home/Miya/.bash_profile  /home/Miya/.bashrc

在if-then语句中,不管命令是否成功执行,你都只有一种选择。如果命令返回一个非零退出状态码,bash shell会继续执行脚本中的下一条命令。当这种情况出现时,如果我们想执行另一组命令怎么办, if-then-else语句可以做到。

3、if-then-else语句

if command
then 
	commands
else 
	commands
fi

当if语句中的命令返回退出状态码0时,then部分中的命令会被执行。当if语句中的命令返回非零退出状态码时,bash shell会执行else部分中的命令。

[root@relay3.mobvista.com:101.251.254.6 shell]#cat test.sh
#!/bin/bash
# testing if-then
username=$1
if grep $username /etc/passwd
then
        echo "$username exits!"
        ls -a /home/$username/.b*
else
        echo "$username is not exits !"
fi

[root@relay3.mobvista.com:101.251.254.6 shell]#sh test.sh Miya
Miya:x:1026:1026::/home/Miya:/bin/bash
Miya exits!
/home/Miya/.bash_logout  /home/Miya/.bash_profile  /home/Miya/.bashrc

[root@relay3.mobvista.com:101.251.254.6 shell]#sh test.sh haha
haha is not exits !

如果我们需要多次判断,可以采用嵌套if-then语句,但其实我们有更便于理解和使用的方法:

if command1
then 
	commands
elif command2
then 
  more commands
fi

elif语句行提供了另一个要测试的命令,这类似于原始的if语句行。如果elif后命令的退出状态码是0,则bash会执行第二个then语句部分的命令。
示例 : 判断存在某个用户名以及该用户的目录是否存在。

[root@relay3.mobvista.com:101.251.254.6 shell]#userdel Miya		//删除了Miya用户
[root@relay3.mobvista.com:101.251.254.6 shell]#grep Miya /etc/passwd
[root@relay3.mobvista.com:101.251.254.6 shell]#ls /home/* | grep Miya			//家目录依旧存在
/home/Miya:

[root@relay3.mobvista.com:101.251.254.6 shell]#cat test.sh
#!/bin/bash
# testing if-then
username=$1
if grep $username /etc/passwd
then
        echo "$username exits!"
        ls -a /home/$username/.b*
elif    ls -d /home/$username
then
        echo -n "$username is not exits!"
        echo " But $username has directory!"
else
        echo "$username is not exits and doesn't have directory!"
fi

[root@relay3.mobvista.com:101.251.254.6 shell]#sh test.sh root
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
root exits!
ls: cannot access /home/root/.b*: No such file or directory
[root@relay3.mobvista.com:101.251.254.6 shell]#sh test.sh Miya
/home/Miya
Miya is not exits! But Miya has directory!
[root@relay3.mobvista.com:101.251.254.6 shell]#sh test.sh haha
ls: cannot access /home/haha: No such file or directory
haha is not exits and doesn't have directory!

需要注意的是,运行if语句中的命令所生成的消息依然会

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值