【第四章】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 ...
随机推荐
- react系列(六)Redux Saga
在Redux中常要管理异步操作,目前社区流行的有Redux-Saga.Redux-thunk等.在管理复杂应用时,推荐使用Redux-Saga,它提供了用 generator 书写类同步代码的能力. ...
- react系列(三)组件间通信
组件间通信 React的基本组件元素是一个个组件,组件之间可能存在关联.组合等关系.不同的组件之间,经常会发生数据传递或者交换,我们称之为组件间通信. 根据传递的复杂程度,可以分为三种情况: 父子间通 ...
- oracle本地安装注意事项
这两天组员在本地windows上安装oracle数据库,安装完各种问题,pl/sql developer以及tns_admin配置以及tnsnames.ora和sqlnet.ora listener. ...
- awk命令用法
awk:把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各种分析处理,是一个强大的文本分析工具,在对数据分析并生成报告时很有优势. awk有3个不同版本: awk.nawk和gawk, ...
- js实现99乘法表的编写(双层for循环与递归方法)
双层for循环实现方法: function nine (num) { ; i <= num; i++){ var str = ''; ; k <= num; k++){ if(i > ...
- PHP curl 携带cookie请求抓取源码,模拟登陆。
公司需要采集一批手机号码,有指定网站.但是需要登陆后才能看到客户号码,手动点击复制太慢,如此就写了以下模拟登陆采集号码程序,分享给大家参考参考. function request_url_data($ ...
- 四、分离分层的 platform驱动
学习目标: 学习实现platform机制的分层分离,并基于platform机制,编写led设备和驱动程序: 一.分离分层 输入子系统.usb设备比驱动以及platform类型的驱动等都体现出分离分层机 ...
- SSH Secure :Algorithm negotiation failed,反复提示输入password对话框
在嵌入式开发中,SSH Secure File Transfer Client 软件使用,方便了windows和linux之间文件拷贝,尤其是多台主机状况下. 最近装了Ubuntu 16.0.4,在V ...
- SaltStack error: No module named 'salt'
启动saltstack的时候出现下面的错误 问题原因 是因为我在centos7中安装了多版本的python导致的 解决方案 将文件下面文件首行更改成python2 [root@saltstack-12 ...
- Django模板语言与视图(view)
常用语法 {{ }}和{% %} 变量相关的用{{}} , 逻辑相关的用{% %} 变量 在Django的模板语言中按此语法使用:{{ 变量名 }}. 当模版引擎遇到一个变量,它将计算这个变量,然后 ...