一、复合条件检查
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/