高级Bash脚本编程(五)笔记

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_43458720/article/details/102745470

if else

sh的流程控制不可为空

1.if

if 语句语法格式:

if condition
then
    command1 
    command2
    ...
    commandN 
fi

2.if else

if else 语法格式:

if condition
then
    command1 
    command2
    ...
    commandN
else
    command
fi

if-elif-else 语法格式:

if condition1
then
    command1
elif condition2 
then 
    command2
else
    commandN
fi

以下实例判断两个变量是否相等:

a=10
b=20
if [ $a == $b ]
then
   echo "a == b"
elif [ $a -gt $b ]
then
   echo "a > b"
elif [ $a -lt $b ]
then
   echo "a < b"
else
   echo "Ineligible"
fi

输出结果:

a < b

if else语句经常与test命令结合使用

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

输出结果:

Two numbers are equal!

猜你喜欢

转载自blog.csdn.net/weixin_43458720/article/details/102745470