【第四章】Shell 条件测试表达式
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 条件测试表达式的更多相关文章
- shell条件测试test
shell条件测试可以通过以下两种方式: test 参数 测试内容 [ 参数 测试内容 ] 一.测试文件类型: test -e 文件名 (测试文件是否存在) [ - ...
- shell条件测试语句实例-测试apache是否开启
终于理解了shell条件测试语句"!="和"-n"的用法区别,于是有了如下的shell脚本,做为练习. 第一种方法:测试apache是否开启?字符串测试 #!/ ...
- 四 Shell条件测试
条件测试操作 在bash的各种流程控制结构中通常要进行各种测试,然后根据测试结果执行不同的操作,有时也会通过与if等条件语句相结合,让我们可以方便的完成判断. 语法格式 test 选项 文件名或目录名 ...
- shell条件测试和流程控制
一.条件测试操作 1.test 用途:测试特定的表达式是否成立,当条件成立时,命令执行后的返回值为0,否则为其他数值 格式:test 表达式 2.常见的测试类型 ①测试文件状态 格式:[ 操作符 文件 ...
- shell条件测试结构
条件测试结构 if/then结构用来判断命令列表的退出状态码是否为0(因为在UNIX惯例, 0表示"成功"), 如果成功的话, 那么就执行接下来的一个或多个命令. 有一个专有命令[ ...
- bash Shell条件测试
3种测试命令: test EXPRESSION [ EXPRESSION ] [[ EXPRESSION ]] 注意:EXPRESSION前后必须有空白字符 bash的测试类型 数值测试: -eq: ...
- shell条件测试
文件状态测试-b filename : 当filename 存在并且是块文件时返回真(返回0)-c filename : 当filename 存在并且是字符文件时返回真-d pathname : 当p ...
- 第四章 函数之lambda 表达式和内置函数
4.5 lambda 表达式 用于表示简单的函数. # 三元运算,为了解决简单的if else的情况,如:if 1 == 1: a = 123else: a = 456# 相当于a = 1 ...
- 《shell条件测试语句,字符串测试apache是否开启》
还得我想了10分钟才明白”!=“和"-n"的用法区别,做个笔记捋一捋 第一种方法:测试apache是否开启?字符串测试 #!/bin/bash web=`/usr/bin/pgre ...
随机推荐
- window下安装composer
1.什么是composer 一个智能的下载工具.比如说我的项目要安装yii框架,而yii是依赖于其他东西的,仅仅安装yii是不够的,这样会导致我的项目也不能正常运行:怎么办呢,我们可以一个一个手动的将 ...
- Oracle中case的第二种用法
procedure P_GetProVerSingInfo_2018(varFileID in varchar2, p_cr1 out refcontent, p_cr2 out refcontent ...
- servlet,过滤器,监听器,拦截器的区别
一.目录 1.概念 2.生命周期 3.职责 4.执行过程 二.内容 概念 1.servlet:servlet是一种运行服务器端的java应用程序,具有独立于平台和协议的特性, 可以动态生成web页面它 ...
- chromium之compiler_specific
直接上代码,将一些编译警告定义成宏 #if defined(COMPILER_MSVC) // Macros for suppressing and disabling warnings on MSV ...
- Resharp常用设置收集整理
F12跳转的问题:
- linux系统中用户
一.用户身份介绍: 1.系统管理员用户,UID(User IDentification) :0, 2.系统用户,UID为1~999,默认的程序都有独立的系统用户负责,运行,进而控制被破坏的范围, 3. ...
- 支付宝支付示例-python
项目演示: 1.输入金额 2.扫码支付: 3.支付完成: 具体操作步骤: 第一步:注册账号 https://openhome.alipay.com/platform/appDaily. ...
- day 22 内置的模块
1.简单的理解模块 写的每一个py文件都是一个模块,还有一些是我们一直在使用的模块. buildins: 内置模块. print, input random 主要是和随机相关的内容: ...
- 不使用IDE,用maven命令打包war项目及出现的问题解决(最全攻略,亲测有效)
第一次在博客园写博客,写的不好大家见谅啊!! 一.首先,在用maven打包之前,一定确认好你使用的jdk版本和maven的版本,最好就是用jdk1.8和maven3.3以上的,版本太低了容易出现一些意 ...
- Linux学习-rsyslog.service :记录登录文件的服务
rsyslog.service 的配置文件:/etc/rsyslog.conf 我们现在知道 rsyslogd 可以负责主机产生的各个信息的登录,而这些信息本身是有『严重等级』之分的, 而且, 这些资 ...