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

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

List of 'if' switches anywhere?

bashif-statementswitch-statementshell-exec

提问by Danijel-James W

Is there a list of all the ifswitches 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 -zin 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 EXPRESSIONSsection:

查看 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 testand [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 ifstatement, but for the testcommand ([is a synonym for the testbuiltin). See help testin bash for a complete list.

它们不是if语句的开关,而是test命令的开关([test内置的同义词)。见help test在bash的完整列表。

回答by icktoofay

It's actually not ifthat's providing those —?it's [, better known by the name of test. help testshould 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 testcommand. 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 ifcommand.

您将它们称为if 开关,但这并不正确。这些是测试,实际上与if命令无关。

The ifcommand merely executes the command you give it, and then if that command returns an exit code of 0, will run the ifportion of the ifstatement. Otherwise, it will run the elseportion (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 ifstatement runs the ls foo.test.txtcommand and the lscommand returns a non-zero because the file does not exist. This causes the ifstatement to execute the elseclause.

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 lscommand returned a 0exit status (since the file exists and the file exists and can be stat'ed by the lscommand.

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

Normally, you shouldn't use the lscommand to test for a file. I merely used it here to show that the ifstatement executes the command, then executed the ifor elseclause depending upon the exit status of that command. If you want to test whether or not a file exists, you should use the test -ecommand instead of lscommand:

通常,您不应使用该ls命令来测试文件。我只是在这里使用它来显示if语句执行命令,然后根据该命令的退出状态执行iforelse子句。如果你想测试一个文件是否存在,你应该使用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 -ewill 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 10958is the inode. Files with the same inodeare two different names for the same file. Thus [and testcommand 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 testand [are builtin, so when you run these commands in BASH, it isn't running /bin/testor /bin/[. However, they're still linkedto each other.

1.在 BASH 中,test[是内置的,所以当你在 BASH 中运行这些命令时,它没有运行/bin/test/bin/[。但是,它们仍然相互关联