【问题标题】:Is there a list of 'if' switches anywhere?是否有任何地方的“if”开关列表?
【发布时间】:2013-10-05 03:42:52
【问题描述】:

是否有用于 Bash 脚本的所有 if 开关的列表?有时我看到有人在使用它,我想知道他们使用的开关实际上是做什么的。

其中一个例子是-z。我知道如何使用它,但我不知道它来自哪里。

if [ -z "$BASH_VERSION" ]; then
    echo -e "Error: this script requires the BASH shell!"
    exit 1
fi

任何参考资料、指南、帖子、答案将不胜感激。

【问题讨论】:

  • [test 命令的简称。试试man test
  • @Barmar 你的意思是help testman test/bin/test 的文档,它支持与内置 test 不同的选项
  • -z[test 的一个选项;它与if无关(直接)。
  • 切线,为了更好地理解实际 if 语句的语法(这实际上并不是关于,正如这里的几个答案所指出的那样),另请参阅 stackoverflow.com/questions/36371221/…

标签: bash if-statement switch-statement shell-exec


【解决方案1】:

实际上不是if 提供了这些——它是[,更广为人知的是testhelp test 应该给你一个它可以采取的所有选项的列表。如果您愿意,也可以查看the standard

【讨论】:

  • -1: test 既是命令/bin/test 也是内置的bash。 man test 为您提供命令的文档,但 [ 是内置命令的同义词。作为不同行为的一个示例,在 OSX 上,内置 bash 支持 -N 测试,但 /bin/test 不支持
【解决方案2】:

它们不是if 语句的开关,而是test 命令的开关([test 内置函数的同义词)。有关完整列表,请参阅 Bash 中的 help test

【讨论】:

    【解决方案3】:

    是的。这些被称为conditional expressions,它们被[[ compound commandtest[ builtin commands 使用([ 只是test 的同义词)。

    阅读Bash Reference Manual6.4 Bash Conditional Expressions 部分,其中包含所有这些开关及其用法的列表。

    【讨论】:

    【解决方案4】:

    查看 Bash 手册页 (man bash)。选项在CONDITIONAL EXPRESSIONS 部分中指定:

    CONDITIONAL EXPRESSIONS
           Conditional expressions are used by the [[  compound  command  and  the
           test  and [ builtin commands to test file attributes and perform string
           and arithmetic comparisons.  Expressions are formed from the  following
           unary  or  binary  primaries.   If any file argument to one of the pri-
           maries is of the form /dev/fd/n, then file descriptor n is checked.  If
           the  file  argument  to  one  of  the  primaries  is one of /dev/stdin,
           /dev/stdout, or /dev/stderr, file descriptor 0, 1, or 2,  respectively,
           is checked.
    
           Unless otherwise specified, primaries that operate on files follow sym-
           bolic links and operate on the target of the link, rather than the link
           itself.
    
           -a file
                  True if file exists.
           ... more options ...
    

    帮助中也有说明:

    $ help [
    [: [ arg... ]
        This is a synonym for the "test" builtin, but the last
        argument must be a literal `]', to match the opening `['.
    

    【讨论】:

    【解决方案5】:

    单方括号 ([ ... ]) 是 test 命令的同义词。如果您查看man page for test,您将看到几乎所有(Bash 可能有一些额外的未在此提及的)各种if 开关,正如您所称的那样。都在一个容易找到的地方。

    如果您使用双方括号 ([[ ... ]]),则您使用的是扩展的 Bash 测试集。这些主要与正则表达式匹配和全局匹配(如果您也有该设置,还有扩展的全局匹配)有关。为此,您必须阅读该 Bash 手册页。

    您称它们为 if 开关,但这并不正确。这些是测试,实际上与if 命令无关。

    if 命令仅执行您给它的命令,然后如果该命令返回退出代码0,将运行if 语句的if 部分。否则,它将运行 else 部分(如果存在)。

    让我们看看这个:

    rm foo.test.txt   # Hope this wasn't an important file
    if ls foo.test.txt
    
    > then
    > echo "This file exists"
    > else
    > echo "I can't find it anywhere.."
    > fi
    ls: foo.test.txt: No such file or directory
    I can't find it anywhere..
    

    if 语句运行ls foo.test.txt 命令,ls 命令返回非零值,因为该文件不存在。这会导致if 语句执行else 子句。

    让我们再试一次...

    touch foo.test.txt   # Now this file exists.
    if ls foo.test.txt   # Same "if/else" statement as above
    
    > then
    > echo "This file exists"
    > else
    > echo "I can't find it anywhere.."
    > fi
    foo.test.txt
    This file exists
    

    这里,ls 命令返回了0 退出状态(因为文件存在并且文件存在并且可以通过ls 命令进行统计。

    通常,您不应使用ls 命令来测试文件。我只是在这里使用它来显示if 语句执行命令,然后根据该命令的退出状态执行ifelse 子句。如果你想测试一个文件是否存在,你应该使用test -e命令而不是ls命令:

    if test -e foo.test.txt  # The same as above, but using "test" instead of "ls"
    then
        echo "This file exists"
    else
        echo "I can't find it anywhere..."
    fi
    

    如果文件存在,test -e 将返回退出状态0。否则,它将返回非零退出状态。

    如果你这样做:

    ls -i /bin/test /bin/[
    
    10958 /bin/[    10958 /bin/test
    

    10958inode。具有相同 inode 的文件是同一文件的两个不同名称。因此[test 命令是软链接1。这意味着您可以使用[ 而不是test

    if [ -e foo.test.txt ]
    then
        echo "This file exists"
    else
        echo "I can't find it anywhere.."
    fi
    

    是不是很眼熟?


    1. 在 Bash 中,test[ 是内置的,因此当您在 BASH 中运行这些命令时,它不会运行 /bin/test/bin/[。但是,它们仍然链接彼此。

    【讨论】:

    • [ 不是 test 的别名。同义词是联机帮助页使用的词。别名具有特定含义(在 bash 手册页中,请阅读 ALIASES 部分)
    • 好的。我会改变它。我只是想找一个词来解释两者之间的联系。
    猜你喜欢
    • 2022-12-01
    • 1970-01-01
    • 2015-05-23
    • 2023-03-27
    • 1970-01-01
    • 2012-08-30
    • 1970-01-01
    • 2010-10-10
    • 1970-01-01
    相关资源
    最近更新 更多