shell test

内置test命令常用操作符号[]表示,将表达式写在[]中,如下:
[ expression ]
或者:
test expression
注意:expression首尾都有个空格
eg: [ ] ;echo ? 测 试 范 围 : 整 数 、 字 符 串 、 文 件 表 达 式 的 结 果 为 真 , 则 t e s t 的 返 回 值 为 0 , 否 则 为 非 0 。 当 表 达 式 的 结 果 为 真 时 , 则 变 量 ? 测试范围:整数、字符串、文件 表达式的结果为真,则test的返回值为0,否则为非0。 当表达式的结果为真时,则变量 ?test00?的值就为0,否则为非0

字符串测试:
test str1 == str2 测试字符串是否相等 =
test str1 != str2 测试字符串是否不相等
test str1 测试字符串是否不为空,不为空,true,false
test -n str1 测试字符串是否不为空
test -z str1 测试字符串是否为空
eg:
name=linzhiling
[ “KaTeX parse error: Expected 'EOF', got '&' at position 9: name” ] &̲& echo ok ; 命令连…name” ] && echo invalid || echo ok
|| 逻辑或,条件不满足,才执行后面的语句
test “$name” == ”yangmi” && echo ok || echo invalid

整数测试:
test int1 -eq int2 测试整数是否相等 equals
test int1 -ge int2 测试int1是否>=int2
test int1 -gt int2 测试int1是否>int2
test int1 -le int2 测试int1是否<=int2
test int1 -lt int2 测试int1是否<int2
test int1 -ne int2 测试整数是否不相等
eg:
test 100 –gt 100
test 100 –ge 100
如下示例两个变量值的大小比较:

-gt表示greater than大于的意思,-le表示less equal表示小于等于。

文件测试:
test -d file 指定文件是否目录
test –e file 文件是否存在 exists
test -f file 指定文件是否常规文件
test –L File 文件存在并且是一个符号链接

test -r file 指定文件是否可读
test -w file 指定文件是否可写
test -x file 指定文件是否可执行

eg:
test -d install.log
test –r install.log
test –f xx.log ; echo $?
[ -L service.soft ] && echo “is a link”
test -L /bin/sh ;echo KaTeX parse error: Expected 'EOF', got '&' at position 16: ? [ -f /root ] &̲& echo “yes” ||…num” –a “KaTeX parse error: Expected 'EOF', got '&' at position 16: num” –ge 520 ] &̲& echo “marry y… pathname”] && echo usable || echo used

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值