本文共 3559 字,大约阅读时间需要 11 分钟。
- 格式1: test 条件表达式- 格式2: [ 条件表达式 ]
举例说明:
111
test -e file 只要文件存在条件为真[ -d /shell01/dir1 ] 判断目录是否存在,存在条件为真[ ! -d /shell01/dir1 ] 判断目录是否存在,不存在条件为真[[ -f /shell01/1.sh ]] 判断文件是否存在,并且是一个普通的文件
[ 33 <> 55 -a 33 -gt $[300/10] ]; echo $?[ 33 -le 44 ] && [ 33 -gt $[999/100] ] ; echo $?
特别说明:
&& 前面的表达式为真,才会执行后面的代码
|| 前面的表达式为假,才会执行后面的代码
; 只用于分割命令或表达式
[root@server ~]# [ $(id -u) -eq 0 ] && echo "the user is admin"[root@server ~]$ [ $(id -u) -ne 0 ] && echo "the user is not admin"[root@server ~]$ [ $(id -u) -eq 0 ] && echo "the user is admin" || echo "the user is not admin"[root@server ~]# uid=`id -u`[root@server ~]# test $uid -eq 0 && echo this is adminthis is admin[root@server ~]# [ $(id -u) -ne 0 ] || echo this is adminthis is admin[root@server ~]# [ $(id -u) -eq 0 ] && echo this is admin || echo this is not adminthis is admin[root@server ~]# su - stu1[stu1@server ~]$ [ $(id -u) -eq 0 ] && echo this is admin || echo this is not adminthis is not admin [ $(id -u) -eq 0 ] && echo 'root用户' || echo '普通用户'
注意:在(( ))中,=表示赋值;==表示判断[root@server ~]# ((1==2));echo $?[root@server ~]# ((1<2));echo $?[root@server ~]# ((2>=1));echo $?[root@server ~]# ((2!=1));echo $?[root@server ~]# ((`id -u`==0));echo $? [root@server ~]# ((a=123));echo $a[root@server ~]# unset a[root@server ~]# ((a==123));echo $?
注意:双引号引起来,看作一个整体;= 和 == 在 [ 字符串 ] 比较中都表示判断[root@server ~]# a='hello world';b=world[root@server ~]# [ $a = $b ];echo $?[root@server ~]# [ "$a" = "$b" ];echo $?[root@server ~]# [ "$a" != "$b" ];echo $?[root@server ~]# [ "$a" !== "$b" ];echo $? 错误[root@server ~]# [ "$a" == "$b" ];echo $?[root@server ~]# test "$a" != "$b";echo $?字符串比较test 表达式[ 表达式 ][[ 表达式 ]]思考:[ ] 和 [[ ]] 有什么区别?单个 [ ] 使用字符串对字符串必须加双引号两个 [[ ]] 不用对字符串变量加双引号两个 [[ ]] 里面可以使用 &&,||, 而单个不行两个 [[ ]] 支持c风格如: a=1; b=2 [[ $a > $b ]] && echo $a || echo $b[root@server ~]# a=[root@server ~]# test -z $a;echo $?[root@server ~]# a=hello[root@server ~]# test -z $a;echo $?[root@server ~]# test -n $a;echo $?[root@server ~]# test -n "$a";echo $?# [ '' = $a ];echo $?-bash: [: : unary operator expected2# [[ '' = $a ]];echo $?0[root@server ~]# [ 1 -eq 0 -a 1 -ne 0 ];echo $?[root@server ~]# [ 1 -eq 0 && 1 -ne 0 ];echo $?[root@server ~]# [[ 1 -eq 0 && 1 -ne 0 ]];echo $?
test exp1
[ exp1 ]
[[ exp1 ]]
(( 整数表达式exp1 ))
-s 判断文件不为空
符号;和&&和||都可以用来分割命令或者表达式
分号(;)完全不考虑前面的语句是否正确执行,都会执行;号后面的内容
&&
符号,需要考虑&&前面的语句的正确性,前面语句正确执行才会执行&&后的内容;反之亦然
||
符号,需要考虑||前面的语句的非正确性,前面语句执行错误才会执行||后内容;反之亦然
如果&&和||一起出现,从左往右依次看,按照以上原则
F:表示false,为假
T:表示true,为真
if [ condition ];then command commandfiif test 条件;then 命令fiif [[ 条件 ]];then 命令fi[ 条件 ] && command
if [ condition ];then command1 else command2fi[ 条件 ] && command1 || command2
if [ condition1 ];then command1 结束 elif [ condition2 ];then command2 结束 else command3fi注释:如果条件1满足,执行命令1后结束;如果条件1不满足,再看条件2,如果条件2满足执行命令2后结束;如果条件1和条件2都不满足执行命令3结束.
if [ condition1 ];then command1 if [ condition2 ];then command2 fi else if [ condition3 ];then command3 elif [ condition4 ];then command4 else command5 fifi注释:如果条件1满足,执行命令1;如果条件2也满足执行命令2,如果不满足就只执行命令1结束;如果条件1不满足,不看条件2;直接看条件3,如果条件3满足执行命令3;如果不满足则看条件4,如果条件4满足执行命令4;否则执行命令5
说明:pattern表示需要匹配的模式case var in 定义变量;var代表是变量名pattern 1) 模式1;用 | 分割多个模式,相当于or command1 需要执行的语句 ;; 两个分号代表命令结束pattern 2) command2 ;;pattern 3) command3 ;; *) default,不满足以上模式,默认执行*)下面的语句 command4 ;;esac esac表示case语句结束
转载地址:http://sfqof.baihongyu.com/