linuxSHELL学习之复合条件检查、case

一、复合条件检查
if-then可以使用布尔逻辑来合并检查条件
*[ condition1 ] && [ condition2 ] 都满足测成立
*[ condition1 ] || [ condition2 ] 任意一个满足即可
[root@t1 ~]# cat t16.sh
#!/bin/bash
#test if file exit and you can write to it
if [ -d $HOME ] && [ -w t7.sh ]
then
  echo "the file exists and you can write to it"
else
  echo "I can't write to the file"
fi
[root@t1 ~]# ./t16.sh
the file exists and you can write to it
二、if-then的高级特征
*双圆括号表示数学表达式
*双方括号表示高级字符串处理函数
1、双圆括号
**表示幂运算、var++表示后增量、var--表示后减量、++var表示前增量、--var表示前减量、<>表示逐位右移
&&表示逻辑与、||表示逻辑或、!表示逻辑否定
[root@t1 ~]# cat t17.sh
#!/bin/bash
#using duble parenthesis
var1=10
if (( var1 ** 2 > 90 ))
then
  (( var2=$var1 ** 2 ))
  echo "the square of $var1 is $var2"
fi
[root@t1 ~]# ./t17.sh
the square of 10 is 100
2、双方括号
双方括号中可以对字符串进行模式匹配
[root@t1 ~]# cat t18.sh
#!/bin/bash
#using pattern matching
if [[ $USER = r** ]]
then
  echo "hello $USER"
else
  echo "sorry i don't know you"
fi
[root@t1 ~]# ./t18.sh
hello root
三、case命令
case命令将制定的变量和不同的模式进行比较。如果变量与模式匹配,shell执行为该模式制定的命令。可以在一列中列出多个模式,使用竖条操作符将每个模式分开。
星号是与任何模式都不匹配的所有值。
[root@t1 ~]# cat t19.sh
#!/bin/bash
#using the case command
case $USER in
root | oracle)
   echo "welcom $USER";;
tesing)
   echo "special testing account";;
haha)
   echo "please don't forget to logout";;
*)
   echo "sorry,you're not allowed to login the system";;
esac
[root@t1 ~]# ./t19.sh
welcom root

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/23655288/viewspace-734375/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/23655288/viewspace-734375/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值