加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – BASH:[](测试)表现不一致

发布时间:2020-12-14 01:06:39 所属栏目:Linux 来源:网络整理
导读:在我的bash测试中有退出状态0的态度: $test -n echo true || echo false- true 而 $test -n "" echo true || echo false- false 这意味着当它根本没有收到任何参数时,它假定为非零. 案例-z正常工作: $test -z echo true || echo false- true$test -z "" ec
在我的bash测试中有退出状态0的态度:

$test -n && echo true || echo false
-> true

$test -n "" && echo true || echo false
-> false

这意味着当它根本没有收到任何参数时,它假定为非零.

案例-z正常工作:

$test -z && echo true || echo false
-> true
$test -z "" && echo true || echo false
-> true

这是预期的行为吗?

解决方法

基本上,您要测试字符串“-z”是否为非空.它是,所以它告诉你真实.实际算法测试使用的是:

  • 0 arguments:

    Exit false (1).

  • 1 argument:

    Exit true (0) if $1 is not null; otherwise,exit false.

  • 2 arguments:

    If $1 is ‘!’,exit true if $2 is null,false if $2 is not null.

    If $1 is a unary primary,exit true if the unary test is true,false
    if the unary test is false.

    Otherwise,produce unspecified results.

引自the POSIX test command specification.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读