#if else语句,共分三种:if...fi ; if ...else ...fi ; if ...elif...else...fi
#if else fi 语句语法
#if [ expression ]
#then
# Statement to be executed if expression is true
#else
# Statement to be executed if expression is not true
#fi
#if elif else fi语句语法:哪一个expression为true就执行哪一个语句,如果全是false则执行else语句
#if [ expression 1 ]
#then
# Statement(s) to be executed if expression 1 is true
#elif [ expression 2 ]
#then
# Statement(s) to be executed if expression 2 is true
#elif [ expression 3 ]
#then
# Statement(s) to be executed if expression 3 is true
#else
# Statement(s) to be executed if no expression is true
#fi
1 a=30 2 b=20 3 if [ $a == $b ] 4 then 5 echo "a is equal to b" 6 elif [ $a -gt $b ] 7 then 8 echo "a is greater than b" 9 elif [ $a -lt $b ] 10 then 11 echo "a is less than b" 12 else 13 echo "None of the condition met" 14 fi
#if else 语句也可以写成一行,以命令的方式运行,如下:
1 if test $[ 2*3 ] -eq $[ 1+5 ] ;then echo "true" ; fi
#if else 语句也经常与test命令结合使用
#test命令用于检查某个条件是否成立与 []类似
1 num1=$[2*3] 2 num2=$[1+5] 3 if test ${num1} -eq ${num2} 4 then 5 echo ‘The two numbers are equal!‘ 6 else 7 echo ‘The two numbers are not equal!‘ 8 fi
时间: 2024-10-09 21:24:16