【问题标题】:-z option inside if condition in shell script-z 选项在 shell 脚本中的 if 条件中
【发布时间】:2011-08-01 10:20:07
【问题描述】:

在使用 shell 脚本编写 if 条件时,有人能告诉我 -z 选项的意义吗?

让我粘贴我正在查看的确切条件

if [[ "x$do_clean_flag" = "x-clean" && -z "$show_me_flag" && "$verify" = true ]]

【问题讨论】:

    标签: shell scripting


    【解决方案1】:

    来自“帮助测试”:

    -z STRING      True if string is empty.
    

    【讨论】:

    • +1 - 测试未初始化变量时也返回 true
    • 谢谢!这对我帮助很大。
    【解决方案2】:

    -z 检查是否定义了 $show_me_flag

    查看测试手册(1)

    【讨论】:

      【解决方案3】:

      -z (something) 表示如果 (something) 为 NULL 则返回 true

      http://unixhelp.ed.ac.uk/CGI/man-cgi?test

      建议零长度也返回 true,我建议在您的机器上进行“人工测试”以检查系统上的确切措辞。

      【讨论】:

      • 没有。 -z NULL 返回 false。 -z 只是测试字符串是否为空。所以-z "" 返回 true。
      • 在我的 Ubuntu 系统上我看到: test -z NULL ;回声$?返回 1. test -z "" ;回声$?返回 0. test -z "something" ;回声$?返回 1。所以零长度返回 false,我错了,但 NULL 返回 true。
      • 实际上,我的 OSX 系统为这些命令提供了与您相同的输出。但我的人测试说The test utility evaluates the expression and, if it evaluates to true, returns a zero (true) exit status; otherwise it returns 1 (false). If there is no expression, test also returns 1 (false). 所以,1 表示假,0 表示真,在这里。 进一步说明:-z string True if the length of string is zero.
      猜你喜欢
      • 1970-01-01
      • 2012-08-17
      • 1970-01-01
      • 2014-06-25
      • 1970-01-01
      • 2012-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多