Shell中的条件判断

语法
if condition
then 
    command1 if condition is true or if exit status of condation is 0
fi
if condition
then
    condition is zero (true - 0) execute all commands up to else statement
else
    if condition is not true then execute all commands up to fi
fi
if condition
then
    condition is zero (true - 0) execute all commands up to elif statement
elif condition1 
then
    condition1 is zero (true - 0) execute all commands up to elif statement  
elif condition2
then
    condition2 is zero (true - 0) execute all commands up to elif statement          
else
    None of the above condtion,condtion1,condtion2 are true (i.e.  all of the above nonzero or false) execute all commands up to fi
fi
condition
  1. 语法: test expr或者[ expr ]

  2. 举例:

    1. if test 2 -gt 1; then echo 0; fi;

    2. if [ 2 -gt 1 ]; then echo 0; fi;

  3. 多个expr可以用&&或者||连接,表示

数字的比较
  1. -eq==: is equal to

  2. -ne!=: is not equal to

  3. -lt: is less than

  4. -le: is less than or equal to

  5. -gt: is greater than

  6. -ge: is greater than or equal to

字符串的比较
  1. string1 = string2: is equal to

  2. string1 != string2: is not equal to

  3. string1: is not null or not defined

  4. -n string1: is not null and does exist

  5. -z string1: string1 is null and does exist

文件和目录
  1. -s file: not empty file

  2. -f file: is file exist or normal file and not a directory

  3. -d dir: is directory exist and not a file

  4. -w file: is writeable file

  5. -r file: is read-only file

  6. -x file: is executable file

逻辑运算符
  1. ! expr: not

  2. expr1 -a expr2: and

  3. expr1 -o expr2: or

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值