形式:if...then...[elif...then]...else...fi
语法格式:
if command | condition
then
commands
elif command | condtion
commands
then
commands
else
commands
fi
代码示例:
#!/bin/bash
# grep -v 过滤掉包含grep子进程
if ps -ef | grep nginx | grep -v grep &> /tmp/null
then
echo "Nginx is Running"
elif ps -ef | grep httpd | grep -v grep &> /tmp/null
then
echo "Apache httpd is Running"
else
echo "Nginx and Httpd is all Stopped"
fi
# 运行结果
~ % sh 04.if_then_else.sh
Nginx and Httpd is all Stopped