博客
关于我
Shell脚本编程test条件测试操作
阅读量:584 次
发布时间:2019-03-09

本文共 798 字,大约阅读时间需要 2 分钟。

test命令

文件测试

测试文件或目录的属性,可以使用以下操作符:

  • -d:判断对象是否为目录
  • -e:判断对象是否存在
  • -f:判断对象是否为文件
  • -r:测试当前用户是否有读取权限
  • -w:测试当前用户是否有写入权限
  • -x:测试当前用户是否有执行权限

**示例:

# 查看某个文件是否是隐藏文件  $ test -d myfolder

**格式说明:

  • 格式1:test -d <文件或目录>
  • 格式2:[<操作符> <文件或目录>]

整数值比较

可以使用以下操作符比较两个整数:

  • -eq:等于
  • -ne:不等于
  • -gt:大于
  • -lt:小于
  • -le:小于或等于
  • -ge:大于或等于

**示例:

# 比较两个数是否相等  $ test 5 -eq 5 => 0  # 或者比较数值  $ test 10 -ge 5 => 0

字符串比较

字符串比较支持两种格式:

格式1:

[
<字符串1>
=
<字符串2>
] [
<字符串1>
!=
<字符串2>
]

格式2:

[ `-z 
<字符串>
` ] [ `-n
<字符串>
` ]

**常用操作符:

  • =:字符串内容相同
  • !=:字符串内容不同
  • -z:字符串内容为空
  • -n:检测字符串是否存在(不为空)

**示例:

# 比较两个字符串是否相等  $ test "hello" = "hello" => 0  # 比较字符串是否为空  $ test -z "hello" => 0

逻辑测试

支持复杂逻辑条件的测试,常用操作符包括:

  • a ||:逻辑或
  • a &&:逻辑与
  • !:逻辑否

**示例:

# 测试文件是否存在并且是隐藏文件  $ test -e myfile && test -d myfile => 0

如果需要同时测试多个条件,可以将多个命令合并到一个 && 语句中。

转载地址:http://vqzsz.baihongyu.com/

你可能感兴趣的文章
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>