形式: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
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐