"if"列表在哪里切换?

Dan*_*s W 30 bash if-statement shell-exec switch-statement

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

例子就是-z这一个.我知道如何使用它,但我不知道它是从哪里得到的.

if [ -z "$BASH_VERSION" ]; then
    echo -e "Error: this script requires the BASH shell!"
    exit 1
fi
Run Code Online (Sandbox Code Playgroud)

任何参考,指南,帖子,答案将不胜感激.

She*_*tJS 33

查看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 ...
Run Code Online (Sandbox Code Playgroud)

它也在帮助中解释:

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

  • 在线手册:[Bash Conditional Expressions](http://www.gnu.org/software/bash/manual/html_node/Bash-Conditional-Expressions.html) (12认同)

Joh*_*web 11

是.这些被称为条件表达式,这些由[[ 复合命令test[ 内置命令([仅仅是同义词test)使用.

阅读部分6.4猛砸条件表达式中的Bash的参考手册,其中包含所有这些开关及其用法的列表.

  • 单页:[6.4 Bash 条件表达式](http://www.gnu.org/software/bash/manual/html_node/Bash-Conditional-Expressions.html) (3认同)

Dav*_* W. 7

单方括号 ( [ ... ]) 是该命令的同义词test。如果您查看test 的手册页,您将看到几乎所有(Bash 可能有一些额外的此处未提及的)您所称的各种if 开关。全部集中在一个易于找到的地方。

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

您称它们为 if switchs,但这并不正确。这些只是测试,与命令无关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..
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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

通常,您不应使用该ls命令来测试文件。我在这里只是用它来表明该if语句执行命令,然后根据该命令的退出状态执行ifor子句。else如果要测试文件是否存在,应该使用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
Run Code Online (Sandbox Code Playgroud)

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

如果你这样做:

ls -i /bin/test /bin/[

10958 /bin/[    10958 /bin/test
Run Code Online (Sandbox Code Playgroud)

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

if [ -e foo.test.txt ]
then
    echo "This file exists"
else
    echo "I can't find it anywhere.."
fi
Run Code Online (Sandbox Code Playgroud)

是不是看起来很眼熟?


1.在 Bash 中,test[是内置的,因此当您在 BASH 中运行这些命令时,它不会运行/bin/test/bin/[。然而,它们仍然相互联系。


Dol*_*000 5

它们不是if语句的开关,而是test命令的开关([test内置的同义词).请参阅help testbash以获取完整列表.