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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:14:21  来源:igfitidea点击:

How to find the options in if conditions of shell

bashshell

提问by John

In one of my shell script, I'm seeing

在我的一个 shell 脚本中,我看到

if [[ ! -d directory1 || ! -L directory ]] ; then

What does -dand -Loption mean here? Where can I find information about the options to use in an ifcondition?

是什么-d-L选项的意思吗?在哪里可以找到有关在条件中使用的选项的信息if

回答by Paused until further notice.

You can do help testwhich 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 bashin the "CONDITIONAL EXPRESSIONS" section.

另请参见man bash条件表达式”部分。

回答by Levon

The -dchecks whether the given directory exists. The -Ltest 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 bashin the terminal.

文件的测试运营商高级Bash脚本编程指南解释了各种选项。这是bash手册页,也可以通过man bash在终端中键入来找到。

回答by camh

bashhas built-in help with the helpcommand. 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 testwere 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 directory1is not a directory orif directoryis 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 中的行是否正确?