是否有用于bash脚本的所有if
个开关的列表?有时我看到有人使用它,我想知道他们使用的切换实际上是做什么的。
示例是此中的-z
。我知道如何使用它,但我不知道它的来源。
if [ -z "$BASH_VERSION" ]; then
echo -e "Error: this script requires the BASH shell!"
exit 1
fi
任何参考,指南,帖子,答案都将不胜感激。
答案 0 :(得分:30)
查看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 `['.
答案 1 :(得分:10)
是。这些被称为conditional expressions,[[
compound command和test
以及[
builtin commands([
test
)的同义词。
阅读6.4 Bash Conditional Expressions的Bash Reference Manual部分,其中包含所有这些切换及其用法的列表。
答案 2 :(得分:5)
它们不是if
语句的开关,但是对于test
命令([
是test
内置的同义词)。有关完整列表,请参阅bash中的help test
。
答案 3 :(得分:3)
实际上并非if
正在提供那些 - [
,更为人所知的是test
。 help test
应该会列出所有可以选择的选项。如果你愿意,你也可以查看the standard。
答案 4 :(得分:3)
单个方括号([ ... ]
)是test
命令的同义词。如果您查看manpage for test,您将看到几乎所有(BASH可能有一些额外未提及的内容)的各种 if开关。所有这一切都很容易找到。
如果使用双方括号([[ ... ]]
),则使用扩展的BASH测试集。这些主要与正则表达式匹配和glob匹配(如果你也有这个集合的扩展的glob匹配)有关。为此,您必须阅读BASH联机帮助页。
你打电话给他们如果开关,但那不是真的正确。这些是 tests ,实际上与if
命令无关。
if
命令只执行您提供的命令,然后如果该命令返回0
的退出代码,则会运行if
的{{1}}部分声明。否则,它将运行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
或if
子句。如果要测试文件是否存在,则应使用else
命令而不是test -e
命令:
ls
如果文件存在,if test -e foo.test.txt # 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
是 inode 。具有相同 inode 的文件是同一文件的两个不同名称。因此10958
和[
命令是软链接的 1 。这意味着您可以使用test
代替[
:
test
看起来很熟悉?
1。在BASH中,if [ -e foo.test.txt ]
then
echo "This file exists"
else
echo "I can't find it anywhere.."
fi
和test
是内置的,因此当您在BASH中运行这些命令时,它不会运行[
或/bin/test
。但是,它们仍然相互链接。