bash 任何地方的“if”开关列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19074542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
List of 'if' switches anywhere?
提问by Danijel-James W
Is there a list of all the if
switches for use in bash scripting? Sometimes I see someone using it and I wonder what the switch they're using actually does.
是否有if
用于 bash 脚本的所有开关的列表?有时我看到有人在使用它,我想知道他们使用的开关实际上是做什么的。
Example is the -z
in this one. I know how to use it, but I don't know where it was derived from.
例子是-z
在这个。我知道如何使用它,但我不知道它来自哪里。
if [ -z "$BASH_VERSION" ]; then
echo -e "Error: this script requires the BASH shell!"
exit 1
fi
Any references, guides, posts, answers would be appreciated.
任何参考,指南,帖子,答案将不胜感激。
回答by SheetJS
Look at the bash manpage (man bash
). The options are specified in the CONDITIONAL EXPRESSIONS
section:
查看 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 ...
It is also explained in the help:
帮助中也有说明:
$ help [ [: [ arg... ]
This is a synonym for the "test" builtin, but the last
argument must be a literal `]', to match the opening `['.
回答by Johnsyweb
Yes. These are called conditional expressionsand these are used by the [[
compound commandand the test
and [
builtin commands([
is simply a synonym for test
).
是的。这些被称为条件表达式,并且这些是由所使用[[
的化合物的命令和test
与[
内置命令([
只是的代名词test
)。
Read section 6.4 Bash Conditional Expressionsof the Bash Reference Manual, which contains a list of all these switches and their usage.
阅读部分6.4猛砸条件表达式中的Bash的参考手册,其中包含所有这些开关及其用法的列表。
回答by Dolda2000
They are not switches for the if
statement, but for the test
command ([
is a synonym for the test
builtin). See help test
in bash for a complete list.
它们不是if
语句的开关,而是test
命令的开关([
是test
内置的同义词)。见help test
在bash的完整列表。
回答by icktoofay
It's actually not if
that's providing those —?it's [
, better known by the name of test
. help test
should give you a list of all options it can take. You could also look at the standard, if you care.
它实际上并不是if
提供那些 -?它的[
名称更广为人知的是test
. help test
应该给你一个它可以采取的所有选项的列表。如果您关心,您也可以查看标准。
回答by David W.
The single square brackets ([ ... ]
) is an synonym of the test
command. If you look at the manpage for test, you will see almost all (BASH might have a few extra not mentioned here) of the various if switchesas you called them. All in one easy to find place.
单个方括号 ( [ ... ]
) 是test
命令的同义词。如果您查看test的联机帮助页,您将看到几乎所有(BASH 可能有一些额外的此处未提及)各种if 开关,如您所称。多合一容易找到的地方。
If you use double square brackets ([[ ... ]]
), you are using an extended BASH set of tests. These mainly have to do with regular expression matching, and glob matching (and extended glob matching if you have that set too). For that, you'll have to read that BASH manpage.
如果您使用双方括号 ( [[ ... ]]
),则您使用的是扩展的 BASH 测试集。这些主要与正则表达式匹配和 glob 匹配(以及扩展的 glob 匹配,如果你也有的话)有关。为此,您必须阅读 BASH 联机帮助页。
You called them if switches, but that's not really correct. These are testsand really have nothing to do with the if
command.
您将它们称为if 开关,但这并不正确。这些是测试,实际上与if
命令无关。
The if
command merely executes the command you give it, and then if that command returns an exit code of 0
, will run the if
portion of the if
statement. Otherwise, it will run the else
portion (if that's present).
该if
命令仅执行您给它的命令,然后如果该命令返回退出代码0
,则将运行语句的if
一部分if
。否则,它将运行该else
部分(如果存在)。
Let's look at this:
让我们看看这个:
$ 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..
The if
statement runs the ls foo.test.txt
command and the ls
command returns a non-zero because the file does not exist. This causes the if
statement to execute the else
clause.
该if
语句运行该ls foo.test.txt
命令并且该ls
命令返回一个非零值,因为该文件不存在。这会导致if
语句执行else
子句。
Let's try that again...
让我们再试一次...
$ 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
Here, the ls
command returned a 0
exit status (since the file exists and the file exists and can be stat'ed by the ls
command.
在这里,ls
命令返回了0
退出状态(因为文件存在并且文件存在并且可以通过ls
命令进行统计。
Normally, you shouldn't use the ls
command to test for a file. I merely used it here to show that the if
statement executes the command, then executed the if
or else
clause depending upon the exit status of that command. If you want to test whether or not a file exists, you should use the test -e
command instead of ls
command:
通常,您不应使用该ls
命令来测试文件。我只是在这里使用它来显示if
语句执行命令,然后根据该命令的退出状态执行if
orelse
子句。如果你想测试一个文件是否存在,你应该使用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
If the file exists, test -e
will return an exit status of 0
. Otherwise, it will return a non-zero exit status.
如果文件存在,test -e
将返回退出状态0
。否则,它将返回非零退出状态。
If you do this:
如果你这样做:
$ ls -i /bin/test /bin/[
10958 /bin/[ 10958 /bin/test
That 10958
is the inode. Files with the same inodeare two different names for the same file. Thus [
and test
command are soft linked1. This means you can use [
instead of test
:
那10958
就是inode。具有相同inode的文件是同一个文件的两个不同名称。因此[
和test
命令是软链接1。这意味着您可以使用[
代替test
:
if [ -e foo.test.txt ]
then
echo "This file exists"
else
echo "I can't find it anywhere.."
fi
Looks familiar?
看起来很熟悉?
1.In BASH, the test
and [
are builtin, so when you run these commands in BASH, it isn't running /bin/test
or /bin/[
. However, they're still linkedto each other.
1.在 BASH 中,test
和[
是内置的,所以当你在 BASH 中运行这些命令时,它没有运行/bin/test
或/bin/[
。但是,它们仍然相互关联。