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. 『ACM C++』 PTA 天梯赛练习集L1 | 036-037

    这几天比较忙,所以随便做做水题了,得赶紧把英剧搞完啊啊啊啊啊啊 ------------------------------------------------L1-036-------------- ...

  2. ABAP术语-Sales Order

    Sales Order 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/14/1104979.html A customer request ...

  3. Spring Security学习笔记(三)

    之前提到过认证后怎么存放用户信息,令牌token是一种方式,session是另一种方式,这里介绍使用spring session data redis存储httpSession. 添加了以上依赖后,我 ...

  4. php5.4以上 mysqli 实例操作mysql 增,删,改,查

    <?php //php5.4以上 mysqli 实例操作mysql header("Content-type:text/html;charset=utf8"); $conn ...

  5. linux 操作之压缩与解压

    压缩与解压 压缩格式:zip , rar , 7z , iso , exe gzip (以下是命令操作) gzip [文件名]  #压缩成gz格式 , 原来的文件消失 , 不支持目录. bzip2 - ...

  6. Hadoop MapReduce自定义数据类型

    一 自定义数据类型的实现 1.继承接口Writable,实现其方法write()和readFields(), 以便该数据能被序列化后完成网络传输或文件输入/输出: 2.如果该数据需要作为主键key使用 ...

  7. python学习笔记:第20天 多继承、MRO C3算法

    目录 一.多继承 二.旧式类的MRO 三.新式类的MRO 四.super 一.多继承 之前已经学习过了继承,当出现了x是⼀种y的的时候,就可以使⽤继承关系,即"is-a" 关系.在 ...

  8. Python 爬虫 七夕福利

    祝大家七夕愉快 妹子图 import requests from lxml import etree import os def headers(refere):#图片的下载可能和头部的referer ...

  9. 手搓一个C语言简单计算器。

    #include <stdio.h> void xing(int shu); void biaoti(int kong,char * title); void zhuyemian(char ...

  10. for循环简单实例(打印乘法表,打印菱形)

    关于for循环的简单应用: 回顾了一下for循环的嵌套: for循环嵌套简单来讲就是一个外圈的for程序里面一个套着一个小的for程序,如果在范围内就来回运行计算,超出了就跳出等待 下面程序为打印九九 ...