博客
关于我
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/

你可能感兴趣的文章
npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
查看>>
npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
查看>>
npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
查看>>
npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
查看>>
npm install CERT_HAS_EXPIRED解决方法
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>
npm install 卡着不动的解决方法
查看>>
npm install 报错 EEXIST File exists 的解决方法
查看>>
npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
查看>>
npm install 报错 Failed to connect to github.com port 443 的解决方法
查看>>
npm install 报错 fatal: unable to connect to github.com 的解决方法
查看>>
npm install 报错 no such file or directory 的解决方法
查看>>
npm install 权限问题
查看>>
npm install报错,证书验证失败unable to get local issuer certificate
查看>>
npm install无法生成node_modules的解决方法
查看>>
npm install的--save和--save-dev使用说明
查看>>
npm node pm2相关问题
查看>>
npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
查看>>
npm run build报Cannot find module错误的解决方法
查看>>
npm run build部署到云服务器中的Nginx(图文配置)
查看>>