Shell脚本——流程控制语句

关键词:选择 (路在自己脚下,是否对错都需自己定夺。)

基本语法结构

1、 if 结构

箴言:只要正确,就要一直向前冲!

F:表示False,为假
T:表示True,为真

1.1 命令格式举例

if [ condition ];then
	 command
	 command
fi
if test 条件; then
	命令
fi
if [[ 条件 ]];then
	命令
fi
[ 条件 ] && command

1.2 流程图表示

在这里插入图片描述

2、if…else结构

箴言:分叉路口,二选一(走在路上难免会遇到选择题,分叉路总得走一条。)

2.1 命令格式举例

if [ condition ];then
	 command1
   else
   	 command2
fi
[ 条件 ] && command1 || command2

2.2 流程图表示

在这里插入图片描述

  • 示例
[root@localhost shell01]# vim 1.sh
[root@localhost shell01]# cat 1.sh
#!/bin/bash


if [ "$1" = "hello" ];then
        echo hello
   else
        echo world
fi
[root@localhost shell01]# chmod +x ./1.sh
[root@localhost shell01]# ./1.sh
world
[root@localhost shell01]# vim 1.sh
[root@localhost shell01]# cat 1.sh
#!/bin/bash


if [ "$1" = "hello" ];then
        echo world
   else
        echo hello
fi
[root@localhost shell01]# chmod +x ./1.sh
[root@localhost shell01]# ./1.sh
hello
[root@localhost shell01]# ./1.sh hello
world

2.3 小试牛刀

让用户自己输入字符串,如果用户输入的是hello,请打印world,否则打印 “请输入hello”

  1. read定义变量
  2. if…else
#!/bin/bash
read -p "请输入一个字符串:" str
if [[ "$str" == "hello" ]];then
        echo "world"
else
        echo "请输入hello"
fi


[root@localhost shell01]# ./2.sh
请输入一个字符串:hello
world
[root@localhost shell01]# ./2.sh
请输入一个字符串:hhh
请输入hello

3、if…elif…else结构

箴言:选择很多,能走的只有一条。

3.1 命令格式举例

if [ condition 1 ];then
	 command 1 	结束
	elif 
	 command 2  结束
	else
	 command 3
fi

注意:
如果条件1满足,执行命令1后结束;如果条件1不满足,再看条件2,如果条件2满足则执行命令后结束;如果条件1和条件2都不满足则执行命令3结束。

3.2流程图表示

在这里插入图片描述

  • 示例
[root@localhost shell01]# vim 3.sh
#!/bin/bash
if [ 1 -eq 2 ];then
        echo 1
 elif [ 0 -ne 0 ];then
        echo 2
 elif [ 0 -ne 2 ];then
        echo 999
 elif [ 0 -ne 2 ];then
        echo 888
else
        echo 3
fi


[root@localhost shell01]# chmod +x 3.sh
[root@localhost shell01]# ./3.sh
999

4、层层嵌套结构

箴言:多次判断,带你走出人生迷雾。

4.1 命令格式举例

if [ condition 1 ];then
	 command 1
	 if [ condition 2 ];then
	 	command 2
	 fi
   else
 	 if [ condition 3 ];then
 	 	command 3
 	 elif [ condition 4 ];then
 	 	command 4
 	 else
 	 	command 5
 	 fi
fi

注意:
如果条件1满足,执行命令1;如果条件2也满足执行命令2,如果不满足就执行命令1结束;
如果条件1不满足,不看条件2;直接看条件3,如果条件3满足执行条件3;如果不满足则看条件4,如果条件4满足执行命令4;否则执行命令5

4.2 流程图表示

image-20240423194436080

5、应用案例

5.1 判断两台主机是否ping通

需求:判断当前主机是否和远程主机是否ping通

思路:

  • 使用哪个命令实现 ( ping -c 次数)
  • 根据命令的执行结果状态来判断是否通 ( $? )
  • 根据逻辑和语法结构来编写脚本 (条件判断或流程控制)
#!/bin/bash
# 该脚本用于判断当前主机是否和远程指定主机互通

# 交互式定义变量,让用户自己决定ping哪个主机

read -p "请输入你要ping的主机的IP:" ip

# 使用ping程序判断主机是否互通
ping -c1 $ip &>/dev/null

if [ $? -eq 0 ];then
        echo "当前主机和远程主机$ip是互通的"
else
        echo "当前主机和远程主机$ip不通"
fi


[root@localhost shell01]# chmod +x host_ping.sh
[root@localhost shell01]# ./host_ping.sh
请输入你要ping的主机的IP:192.168.x.x
当前主机和远程主机192.168.x.x是互通的
[root@localhost shell01]# ./host_ping.sh
请输入你要ping的主机的IP:192.168.1.2
当前主机和远程主机192.168.1.2不通

5.2 判断一个进程是否存在

需求:判断web服务器中httpd进程是否存在

思路:

  • 查看进程的相关命令 ( ps 、pgrep )
  • 根据命令的返回状态值来判断进程是否存在
  • 根据逻辑用脚本语言实现

补充命令:

pgrep命令:以名称为依据从运行进程队列中查找进程,并显示查找到的进程id选项

-o:仅显示找到的最小(起始)进程号
-n:仅显示找到的最大(结束)进程号
-l:显示进程名称
-p:指定父进程号; pgrep -p 4764 查看父进程下的子进程id
-g:指定进程组
-t:指定开启进程的终端
-u:指定进程的有效用户ID

#!/bin/bash
#判断一个程序(httpd)的进程是否存在
pgrep httpd &>/dev/null
if [ $? -ne 0 ];then
        echo "当前httpd进程不存在"
else
        echo "当前httpd进程存在"
fi

或者 
test $? -eq 0 && echo "当前httpd进程存在" || echo "当前httpd进程不存在"

5.3 判断一个服务是否正常

需求:判断门户网站是否能够正常访问

思路:

  • 可以判读进程是否存在,用 /etc/init.d/httpd status 判断状态等方法
  • 最好的办法就是直接去访问一下,通过访问成功和失败的返回值来判断
#!/bin/bash
#判断门户网站是否能够正常提供服务

#定义变量
web_server=www.baidu.com
#访问网站
wget -P /shell/ $web_server &>/dev/null
[ $? -eq 0 ] && echo "当前门户网站是ok" && rm -f /shell/index.* || echo "当前网站服务不ok,请立
刻处理"


[root@localhost ~]# chmod +x ./web_server_check.sh
[root@localhost ~]# ./web_server_check.sh
当前门户网站是ok

5.4 判断用户是否存在

#!/bin/bash
#判断用户是否存在

read -p "请输入一个用户名:" user_name
id $user_name &>/dev/null
if [ $? -eq 0 ];then
        echo "该用户存在"
else
        echo "该用户不存在"
fi


[root@localhost shell01]# chmod +x user.sh
[root@localhost shell01]# ./user.sh
请输入一个用户名:aileen
该用户存在
[root@localhost shell01]# ./user.sh
请输入一个用户名:joseph
该用户不存在
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值