Shell if 语句通过表达式与关系运算符判断表达式真假来决定执行哪个分支。有三种 if ... else 语句:
if ... fi 语句;
if ... else ... fi 语句;
if ... elif ... else ... fi 语句。
首先需要记住if和fi成对出现。先看一个简单脚本。
#表达式语句结构
if <条件表达式> ;then
指令
fi
#上下两条语句等价
if <条件表达式>
then
指令
fi
#判断输入是否为yes,没有判断其他输入操作
[root@promote ~]# cat testifv1.0.sh
#!/bin/bash
read -p "please input yes or no: " input
if [ $input == "yes" ] ;then
echo "yes"
fi
#请勿直接回车
[root@promote ~]# bash testifv1.0.sh
please input yes or no: yes
yes
[root@promote ~]#
if语句条件表达式为真时,执行