shell中条件测试的三种格式:

格式1: test 条件表达式
格式2: [ 条件表达式 ]
格式3: [[ 条件表达式 ]]

使用test:
[root@host- ~]# test -f file && echo true || echo false
false
[root@host- ~]# touch file
[root@host- ~]# test -f file && echo true || echo false
true [root@host- ~]# man test #可以查看帮助
-z 测试如果字符串长度是否为零:
[root@host- ~]# test -z "yanglt" && echo || echo [root@host- ~]# char="yanglt"
[root@host- ~]# test -z "$char" && echo || echo [root@host- ~]# char=""
[root@host- ~]# test -z "$char" && echo || echo 使用[]:
[root@host- tmp]# touch yanglt.txt
[root@host- tmp]# [ -f /tmp/yanglt.txt ] && echo || echo 判断逻辑表达式写法:
[root@host- tmp]# [ -f /tmp/yanglt.txt ] && echo #成功输出1 [root@host- tmp]# [ -f /tmp/yanglt.txt ] || echo #不成功输出0
[root@host- tmp]# [ -f /tmp/yanglt123.txt ] || echo []命令和test命令的选项时通用的,所以[] 也可以用 man test 命令获取帮助

使用[[]]:
[root@host- tmp]# [[ -f /tmp/yanglt.txt ]] && echo || echo

:[[]] 表达式和 []和test 测试表达式区别:
[[]]:可以使用通配符进行模式匹配,可以使用&& 、 || 、> 、< 等操作符
但不能用于[]中,[]中一般使用-a 、-o 、-gt(用于整数) 、-lt(用于整数)

查看帮助:

test、[]、[[]]这些操作符的用法,通过help test 或 man test查询得到帮助,完整的[]、[[]] 用法可以通过man bash来获得帮助。

一、文件测试表达

常用的文件测试操作符:

(1) 普通文件(测试文件类型)

[root@host- ~]# touch yanglt
[root@host- ~]# ls -l |grep "yanglt$"
-rw-r--r-- root root 6月 : yanglt
[root@host- ~]# [ -f yanglt ] && echo || echo ()目录文件 (测试目录类型) [root@host- ~]# mkdir yanglt01 [root@host- ~]# [ -f yanglt01 ] && echo || echo #测试是否为普通文件 是为1,不是为0 [root@host- ~]# [ -e yanglt01 ] && echo || echo #测试yanglt001是否存在 [root@host- ~]# [ -d yanglt01 ] && echo || echo #测试是否为目录 [root@host- ~]# [ -d yanglt ] && echo || echo [root@host- ~]# ()测试文件属性
[root@host- ~]# ls -l yanglt
-rw-r--r-- root root 6月 : yanglt
[root@host- ~]# ls -l yanglt01
总用量
[root@host- ~]# [ -r yanglt ] && echo ||echo [root@host- ~]# [ -w yanglt ] && echo || echo [root@host- ~]# [ -x yanglt ] && echo || echo [root@host- ~]# chmod yanglt
[root@host- ~]# ls -l yanglt
---------x root root 6月 : yanglt #我们发现用户权限没有读写,但是下边依然可以返回1,这就是root用户比较特殊的地方
[root@host- ~]# echo 'text' > yanglt
[root@host- ~]# cat yanglt
text
[root@host- ~]# [ -w yanglt ] && echo || echo [root@host- ~]# [ -x yanglt ] && echo || echo [root@host- ~]# [ -r yanglt ] && echo ||echo 当我们切换用户后按照以上方法再次操作:
[root@host- ~]# su - yanglt1
[yanglt1@host- ~]$ touch yanglt001
[yanglt1@host- ~]$ ls -l yanglt001
-rw-rw-r-- yanglt1 yanglt1 6月 : yanglt001
[yanglt1@host- ~]$ [ -x yanglt001 ] && echo || echo [yanglt1@host- ~]$ [ -w yanglt001 ] && echo || echo [yanglt1@host- ~]$ [ -r yanglt001 ] && echo || echo [yanglt1@host- ~]$ chmod yanglt001
[yanglt1@host- ~]$ [ -x yanglt001 ] && echo || echo [yanglt1@host- ~]$ ls -l
总用量
---------x yanglt1 yanglt1 6月 : yanglt001 #我们可以看到没有该用户读写执行权限,下边的测试结果可见
[yanglt1@host- ~]$ [ -x yanglt001 ] && echo || echo
#因为文件所属主没有执行权限
[yanglt1@host- ~]$ [ -w yanglt001 ] && echo || echo [yanglt1@host- ~]$ [ -r 4yanglt001 ] && echo || echo [yanglt1@host- ~]$ 测试文件的读写执行属性,不仅要看rwx,还要看当前用户是否有操作该文件的对应权限

二、字符串测试表达式

对于字符串的测试,一定要将字符串加双引号之后在进行比较

比较符号“!=”和“=”两端要有空格,比较两个字符串是否相同

[root@host- ~]# [ -n "abc" ]&& echo  || echo     #字符串长度不为零

[root@host- ~]# test -n "abc" && echo  || echo   

[root@host- ~]# var="yanglt"
[root@host- ~]# test -z "$var" && echo || echo #字符串长度为零的时候为真,现在不为真返回0 [root@host- ~]# [ "abc" = "abc"] #括号两边需要加空格
-bash: [: 缺少 `]'
[root@host- ~]# [ "abc" = "abc" ]
[root@host- ~]# [ "abc" = "abc" ] && echo [root@host- ~]# [ "abc" != "abc" ] && echo #成立返回1,不成立返回0
[root@host- ~]# [ "$var" != "abc" ] && echo [root@host- ~]# [ "$var" = "abc" ] && echo 等号两边没有空格会出现以下判断错误:
[root@host- ~]# [ "abc" = "" ] && echo || echo [root@host- ~]# [ "abc"="" ] && echo || echo [root@host- ~]#

字符串不加双引号出现逻辑的错误:
[root@host-131 ~]# var=""
[root@host-131 ~]# [ -n "$var" ] && echo 1 || echo 0
0
[root@host-131 ~]# [ -n $var ] && echo 1 || echo 0
1
[root@host-131 ~]# [ -z $var ] && echo 1 || echo 0
1
[root@host-131 ~]# [ -z "$var" ] && echo 1 || echo 0
1
[root@host-131 ~]#

三、数值测试表达式

有关[]、[[]]、(())用法的小结:

  • 整数加双引号的比较是对的。
  • [[]]中用类似-eq等的写法是对的,[[]]中用类似>、<的写法也可能不对,有可能会只比较第一位,逻辑结果不对。
  • []中用类似>、<的写法在语法上虽然可能没错,但逻辑结果不对,可以使用=、!=正确比较。
  • (())中不能使用类似-eq等的写法,可以使用类似>、<的写法。

参考文件:跟老男孩学shell

【第四章】Shell 条件测试表达式的更多相关文章

  1. shell条件测试test

    shell条件测试可以通过以下两种方式: test   参数    测试内容 [ 参数  测试内容 ] 一.测试文件类型: test  -e   文件名          (测试文件是否存在) [ - ...

  2. shell条件测试语句实例-测试apache是否开启

    终于理解了shell条件测试语句"!="和"-n"的用法区别,于是有了如下的shell脚本,做为练习. 第一种方法:测试apache是否开启?字符串测试 #!/ ...

  3. 四 Shell条件测试

    条件测试操作 在bash的各种流程控制结构中通常要进行各种测试,然后根据测试结果执行不同的操作,有时也会通过与if等条件语句相结合,让我们可以方便的完成判断. 语法格式 test 选项 文件名或目录名 ...

  4. shell条件测试和流程控制

    一.条件测试操作 1.test 用途:测试特定的表达式是否成立,当条件成立时,命令执行后的返回值为0,否则为其他数值 格式:test 表达式 2.常见的测试类型 ①测试文件状态 格式:[ 操作符 文件 ...

  5. shell条件测试结构

    条件测试结构 if/then结构用来判断命令列表的退出状态码是否为0(因为在UNIX惯例, 0表示"成功"), 如果成功的话, 那么就执行接下来的一个或多个命令. 有一个专有命令[ ...

  6. bash Shell条件测试

    3种测试命令: test EXPRESSION [ EXPRESSION ] [[ EXPRESSION ]]  注意:EXPRESSION前后必须有空白字符 bash的测试类型 数值测试: -eq: ...

  7. shell条件测试

    文件状态测试-b filename : 当filename 存在并且是块文件时返回真(返回0)-c filename : 当filename 存在并且是字符文件时返回真-d pathname : 当p ...

  8. 第四章 函数之lambda 表达式和内置函数

    4.5 lambda 表达式 用于表示简单的函数. # 三元运算,为了解决简单的if else的情况,如:if 1 == 1:    a = 123else:    a = 456# 相当于a = 1 ...

  9. 《shell条件测试语句,字符串测试apache是否开启》

    还得我想了10分钟才明白”!=“和"-n"的用法区别,做个笔记捋一捋 第一种方法:测试apache是否开启?字符串测试 #!/bin/bash web=`/usr/bin/pgre ...

随机推荐

  1. vue使用iview Timeline 时间轴不显示问题

    vue Timeline 时间轴不显示渲染的效果 官网代码 <Timeline pending> <TimelineItem>发布1.0版本</TimelineItem& ...

  2. oracle 之分析函数 over (partition by ...order by ...)

    一:分析函数overOracle从8.1.6开始提供分析函数,分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是对于每个组返回多行,而聚合函数对于每个组只返回一行. 1.分析函数和聚合函数的 ...

  3. oracle sqldrl命令与以及ctl文件

    具体操作如下: 第一步:先编辑好数据控制文件 xx.ctl,如test.ctl options(skip=1)   --跳过第一行(看实际情况) load data infile 'C:\Users\ ...

  4. CGAffineTransform 视频旋转(转)

    记录下视频旋转 ////////////////////////////////////////////// - (void)test:(NSURL *)url transformUrl:(NSURL ...

  5. C++ ACM基础

    一.C++结构体 #include <iostream> using namespace std; struct Point{ int x; int y; Point(int x=0,in ...

  6. Waltz of love

    Waltz of love Love me tenderly Love me softly Close your eyes,fling to the dangcing hall Follow your ...

  7. java 递归打印20个斐波那契数

    class Test { public static void main(String[] args) { // feibo j=new feibo(); for (int n = 1; n < ...

  8. AtCoder Grant Contest 10.F 博弈

    题意:给定一棵树,每个节点有个权值,Alice和Bob轮流进行操作,给定游戏起点指针指向节点C.不断进行下述操作. 1.将当前节点权值-1,然后将指针从该节点移动到相邻节点,出现一方不能移动(即指针指 ...

  9. 如何在VMware Fusion中导入windows下的虚拟机

    最近换了新款的mbp,因为偷懒,便将之前在windows台式机上的虚拟机搬了过来. 特此记录下搬运过程,方便以后查看. 一 操作过程 安装激活VMware 常规操作,无需赘言 拷贝windows下虚拟 ...

  10. ERROR oslo_service.service PlacementNotConfigured 解决办法

    PlacementNotConfigured: This compute is not configured to talk to the placement service 原因:官方文档中遗漏了- ...