一、if-then-else语句
- 格式:
if commands|condition then commands else commands fi
举例:
#!/bin/bash # if ps ux|grep MySQL|grep -v grep &> /dev/null then echo "MySQL if running" else echo "MySQL is stopped" fi
二、嵌套if
- 格式
if commands|condition then commands elif commands|condition then commands ... else commands fi # 举例 #!/bin/bash # if ps ux|grep MySQL|grep -v grep &> /dev/null then echo "MySQL if running" elif ps ux|grep oracle|grep -v grep &> /dev/null then echo "oracle is running" else echo "ALL databases are stopped" fi
三、复合条件
- 格式
if condition1 && condition2 then commands else commands fi if condition1 || condition2 then commands else commands fi