bash 如何在shell的if条件中找到选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11008084/
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
How to find the options in if conditions of shell
提问by John
In one of my shell script, I'm seeing
在我的一个 shell 脚本中,我看到
if [[ ! -d directory1 || ! -L directory ]] ; then
What does -d
and -L
option mean here? Where can I find information about the options to use in an if
condition?
是什么-d
和-L
选项的意思吗?在哪里可以找到有关在条件中使用的选项的信息if
?
回答by Paused until further notice.
You can do help test
which will show most of the options accepted by the [[
command.
您可以这样做help test
,这将显示[[
命令接受的大多数选项。
You can also do help [
which will show additional information. You can do help [[
to get information on that type of conditional.
您也可以这样做help [
,这将显示附加信息。您可以这样做help [[
以获取有关该类型条件的信息。
Also see man bash
in the "CONDITIONAL EXPRESSIONS" section.
另请参见man bash
“条件表达式”部分。
回答by Levon
The -d
checks whether the given directory exists. The -L
test for a symbolic link.
该-d
检查是否给定的目录是否存在。-L
符号链接的 测试。
The File test operatorsfrom the Advanced Bash-Scripting Guideexplain the various options. And here is the man page for bashwhich can also be found by typing man bash
in the terminal.
该文件的测试运营商从高级Bash脚本编程指南解释了各种选项。这是bash的手册页,也可以通过man bash
在终端中键入来找到。
回答by camh
bash
has built-in help with the help
command. You can easily find out the options to a bash built-in using help
:
bash
具有help
命令的内置帮助。您可以使用以下命令轻松找到内置 bash 的选项help
:
$ help [[
...
Expressions are composed of the same primaries used by the `test' builtin
...
$ help test
test: test [expr]
Evaluate conditional expression.
...
[the answer you want]
回答by David W.
In Bourne shell, [
and test
were linked to the same executable. Thus, you can find a lot of the various testsavailable in the testmanpage.
在Bourne shell中,[
并test
分别连接到同一个可执行文件。因此,您可以在测试联机帮助页中找到许多可用的各种测试。
This:
这个:
if [[ ! -d directory1 || ! -L directory ]] ; then
is saying if directory1
is not a directory orif directory
is not a link.
是说,如果directory1
不是目录或者如果directory
不是链接。
I believe the correct syntax should be:
我相信正确的语法应该是:
if [[ ! -d $directory1 ] || [ ! -L $directory ]] ; then
or
或者
if [[ ! -d $directory1 -o ! -L $directory ]] ; then
Is the line in your OP correct?
您的 OP 中的行是否正确?