if judgment

if … else

#!/bin/sh
a=10
b=20
if [ $a == $b ]    # if与[ ]有空格,"["  、 "]"与字符都有空格
then echo "a is equal to b"
else echo "a is not equal to b"
fi

if … elif … fi statements can evaluate multiple conditions

#!/bin/sh
a=10
b=20
if [ $a == $b ];then
    echo "a is equal to b"
elif [ $a -gt $b ];then
    echo "a is greater than b"
elif [ $a -lt $b ];then
    echo "a is less than b"
else
    echo "None of the condition met"
fi

if … else statements are also often used in conjunction with the test command

num1=$[2*3]
num2=$[1+5]
if test $[num1] -eq $[num2];then
    echo 'The two numbers are equal!'
else
    echo 'The two numbers are not equal!'
fi

The test command is used to check whether a condition is true, similar to square brackets ([ ])

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325446244&siteId=291194637
Recommended