Shell IF 表达式
IF 语句的基本结构
基本语法格式
if [ condition ]; then
# 条件为真时执行的命令
fi
或者多行写法:
if [ condition ]
then
# 条件为真时执行的命令
fi
完整结构(包含 else 和 elif)
if [ condition1 ]; then
# condition1 为真时执行
elif [ condition2 ]; then
# condition2 为真时执行
else
# 所有条件都为假时执行
fi
IF 语句应用
单分支
[root@shell bin]# cat test_sshd
#!/bin/bash
systemctl is-active sshd &>/dev/null
if (( $?!=0 ));then
echo "Sshd is not running"
echo -n "Start sshd ... "
systemctl start sshd && echo Success.
fi
双分支
使用命令退出状态码$?
来判断sshd服务是否运行
[root@server bin 09:49:46]# vim ssh_ctl_if_1.sh
[root@server bin 09:52:07]# cat ssh_ctl_if_1.sh
#!/bin/bash
systemctl is-active sshd &>/dev/null
return_count=$?
if (( return_count != 0 ));then
echo "Sshd is not running."
echo -n "Starting sshd ... "
systemctl start sshd && echo Success || echo Fail
else
echo "Sshd is running."
#echo -n "Stopping sshd ... "
#systemctl stop sshd && echo Success || echo Fail
fi
[root@server bin 09:52:11]# chmod +x ssh_ctl_if_1.sh
[root@server bin 09:52:28]# ./ssh_ctl_if_1.sh
Sshd is running.
[root@server bin 09:52:32]# systemctl stop sshd
[root@server bin 09:52:45]# ./ssh_ctl_if_1.sh
Sshd is not running.
Starting sshd ... Success
[root@server bin 09:52:55]# systemctl is-active sshd
active
使用命令替换$()
来来判断sshd服务是否运行
[root@server bin 09:53:05]# cp ssh_ctl_if_1.sh ssh_ctl_if_2.sh
[root@server bin 10:03:23]# ls
ssh_ctl_if_1.sh ssh_ctl_if_2.sh
[root@server bin 10:03:24]# vim ssh_ctl_if_2.sh
[root@server bin 10:03:48]# cat ssh_ctl_if_2.sh
#!/bin/bash
status="$(systemctl is-active sshd)"
if (( status == "inactive" ));then
echo "Sshd is not running."
echo -n "Starting sshd ... "
systemctl start sshd && echo Success || echo Fail
else
echo "Sshd is running."
#echo -n "Stopping sshd ... "
#systemctl stop sshd && echo Success || echo Fail
fi
[root@server bin 10:08:40]# systemctl stop sshd
[root@server bin 10:08:49]# systemctl is-active sshd
inactive
[root@server bin 10:08:52]# ./ssh_ctl_if_2.sh
Sshd is not running.
Starting sshd ... Success
[root@server bin 10:08:59]# systemctl is-active sshd
active
多分支
if elif else
[root@server bin 10:20:42]# cp ssh_ctl_if_2.sh ssh_ctl_if_3.sh
[root@server bin 10:21:08]# vim ssh_ctl_if_3.sh
[root@server bin 10:21:35]# cat ssh_ctl_if_3.sh
#!/bin/bash
if [ "$1" = "start" ];then
systemctl $1 sshd
elif [ "$1" = "stop" ];then
systemctl $1 sshd
elif [ "$1" = "status" ];then
systemctl $1 sshd
else
echo "Usage: $0 start|stop|status"
fi
[root@server bin 10:21:38]# ./ssh_ctl_if_3.sh status
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since 五 2025-10-10 10:08:56 CST; 13min ago
... ...
[root@server bin 10:22:04]# ./ssh_ctl_if_3.sh stop
[root@server bin 10:22:11]# ./ssh_ctl_if_3.sh status
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: inactive (dead) since 五 2025-10-10 10:22:11 CST; 2s ago
... ...
[root@server bin 10:22:13]# ./ssh_ctl_if_3.sh start
[root@server bin 10:22:18]# ./ssh_ctl_if_3.sh status
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since 五 2025-10-10 10:22:18 CST; 2s ago
... ...
也可以减少代码量,优化一下
[root@server bin 10:23:58]# vim ssh_ctl_if_4.sh
[root@server bin 10:24:21]# cat ssh_ctl_if_4.sh
#!/bin/bash
if [ "$1" = "start" -o "$1" = "stop" -o "$1" = "status" ];then
systemctl $1 sshd
else
echo "Usage: $0 start|stop|status"
fi
根据上一节的内容,-o,表示或者
综合练习
需求:每3分钟检查一次系统可用内存,如果空闲内存低于100M时给root用户发邮件。
- 每3分钟运行一次脚本 cron定时任务完成
- 检查内存 free -m | awk ‘/Mem/ {print $4}’
- 发邮件 mailx,具体命令示例 mail -s “test” root < /etc/hosts
[root@server bin 10:47:34]# vim check_memory.sh
[root@server bin 10:47:53]# cat check_memory.sh
#!/bin/bash
memory_available_size=$(free -m | awk '/Mem/ {print $NF}')
memory_min_size=10000
if (( memory_free_size < memory_min_size ));then
echo "Current available memory size is ${memory_available_size}" | \
mail -s "memory size is lower" root
fi
[root@server bin 10:49:11]# chmod +x check_memory.sh
# 为了方便测试,这里我们选择每一分钟检查一次
[root@server bin 10:49:56]# crontab -e
[root@server bin 10:51:22]# crontab -l
* * * * * /root/bin/check_memory.sh
# 先安装mailx软件包,使用这个软件包来查看邮件
[root@server bin 10:52:38]# yum install -y mailx
[root@server bin 10:53:08]# mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/root": 2 messages 2 new
>N 1 (Cron Daemon) Fri Oct 10 10:52 25/866
N 2 (Cron Daemon) Fri Oct 10 10:53 25/866
& New mail has arrived.
Held 2 messages in /var/spool/mail/root
您在 /var/spool/mail/root 中有邮件
[root@server bin 10:54:27]# mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/root": 3 messages 1 new 3 unread
U 1 (Cron Daemon) Fri Oct 10 10:52 26/876
U 2 (Cron Daemon) Fri Oct 10 10:53 26/876
>N 3 (Cron Daemon) Fri Oct 10 10:54 25/866
总结
- 基本结构:
if [ condition ]; then ... fi
- 测试命令:
test
或[ ]
或[[ ]]
- 文件测试:
-f
,-d
,-e
等 - 字符串比较:
=
,!=
,-z
,-n
- 数值比较:
-eq
,-ne
,-gt
,-lt
等 - 逻辑运算:
&&
,||
,!