在Shell脚本中,if语句用于根据特定条件执行代码块
#!/bin/bash
num=10
if [ $num -eq 10 ]; then
echo "Number is 10."
elif [ $num -lt 10 ]; then
echo "Number is less than 10."
else
echo "Number is greater than 10."
fi
在这个示例中,我们首先设置了一个变量num,并将其值设置为10。然后,我们使用if语句检查num是否等于10。如果条件为真,我们输出"Number is 10.“。如果条件为假,我们使用elif检查num是否小于10。如果这个条件为真,我们输出"Number is less than 10.”。如果前面的所有条件都不满足,我们使用else输出"Number is greater than 10."。最后,我们使用fi表示if语句块的结束。
注意,我们在条件表达式两侧使用了方括号[和],并在操作符和值之间有空格。这是Shell脚本中的语法规则。