-n 和 -a 选项在 bash if 语句中有何作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14900426/
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 23:20:22  来源:igfitidea点击:

What do the -n and -a options do in a bash if statement?

bash

提问by surfi

What function do the -aand -noptions perform in the following bash ifstatement?

-a-n选项在以下 bashif语句中执行什么功能?

if [ -n "" -a -n "" -a -n "" ]; then
    REFNAME=$(basename )
else

Are -aand -nso called primaries?

-a-n所谓的初选

Does -a filemean "True if file exists."?

是否-a file意味着“如果文件存在则为真。”?

采纳答案by sorpigal

Nitpicking

吹毛求疵

The switches -aand -nare not strictly part of a bashifstatement in that the ifcommand does not process these switches.

开关-a-n严格来说不是bashif语句的一部分,因为该if命令不处理这些开关。

What are primaries?

什么是初选?

I call them "switches", but the bashdocumentation that you linked to refers to the same thing as "primaries" (probably because this is a common term used when discussing parts of a boolean expression).

我称它们为“开关”,但bash您链接到的文档与“主要”指代相同的事物(可能是因为这是讨论布尔表达式的一部分时使用的常用术语)。

Background and docs

背景和文档

In shscripts ifis a command that takes a command as its argument, executes it and tests its return code. If the return code is 0the block of code following thenis executed up until the closing fior (if supplied) the following else. If the return code was not 0and an elsestatement was supplied then the block of code following elseis executed up until the closing fi.

sh脚本中if是一个命令,它将命令作为参数,执行它并测试它的返回码。如果返回代码是0以下代码块,then则执行直到关闭fi或(如果提供)以下代码else。如果没有返回代码0并且else提供了一条语句,else则执行下面的代码块直到结束fi

You can see this effect by passing ifthe command trueor the command false, which are simple commands that do nothing and return 0and non-0respectively.

你可以通过传递ifcommandtrue或 command来看到这种效果false,它们是简单的命令,什么都不做00分别返回和非。

if true ; then echo true was true ; else echo true was false ; fi
if false ; then echo false was true ; else echo false was false ; fi

In the sample code you provided the command that you're passing to ifis [, which is also sometimes known as test. It is this command which takes the switches you're asking about. In bashthe testcommand will be a built-in command; try type [to learn its type. For built-in commands helpwill show usage, so also run help [to see documentation. Your system probably also has a /bin/[and a /bin/testand if you man testyou can see the manuals for those. Although the behavior of the built-in testmay not be identical to the behavior documented in the man pages, which is likely more verbose than the simple description you'll get from help [, it will probably describe the behavior of the built-in [command fairly accurately.

在示例代码中,您提供了要传递给ifis的命令,[有时也称为test. 正是这个命令采用了您所询问的开关。在bashtest命令将是一个内置的命令; 尝试type [学习它的类型。对于内置命令help将显示用法,因此也运行help [以查看文档。您的系统可能还有一个/bin/[和一个/bin/test,如果man test您可以查看它们的手册。尽管内置程序的行为test可能与手册页中记录的行为不同,这可能比您从中获得的简单描述更冗长help [,但它可能会描述内置程序的行为[命令相当准确。

The behavior of -a and -n

-a 和 -n 的行为

Knowing that the command you're running is testwe can consult help testor man testand read its usage. This will show that-ntests the following argument and evaluates to true if it is not an empty string.

知道您正在运行的命令是test我们可以查阅help testman test阅读其用法。这将显示-n测试以下参数,如果它不是空字符串,则计算结果为 true。

In the documentation of testyou will also see a the switch -e. This switch tests the following argument and evaluates to true if that argument is a file or directory that exists. More useful still is the -fswitch which evaluates to true if the following argument exists and is a regular file (as opposed to a directory or a block device, or whatever).

test您的文档中,您还将看到一个 switch -e。此开关测试以下参数,如果该参数是存在的文件或目录,则计算结果为真。更有用的是-f如果以下参数存在并且是常规文件(而不是目录或块设备或其他任何东西),则计算结果为真的开关。

The source of your confusion is probably that there can be twoforms of -a: Unary and binary. When -ais used in a unary context, that is with one following argumentbut no preceding arguments, it treats its argument as a file and tests for its existence, just like the -eswitch. However, when -ais used in a binary context, that is with one argument before it and one argument after it, it treats its arguments as other conditions and acts as a boolean AND operator.

您混淆的根源可能是可能有两种形式-a:一元和二元。当-a在一元上下文中使用时,即有一个后面的参数没有前面的参数时,它将其参数视为一个文件并测试它的存在,就像-e开关一样。但是,当-a在二进制上下文中使用时,即前面有一个参数,后面有一个参数,它将其参数视为其他条件并充当布尔 AND 运算符。

In the interests of portability it is important to note that unary -ais a non-standard extension which won't be foundin POSIX. It is available in bashand ksh, however, so usage is probably widespread.

为了可移植性,重要的是要注意一元-a是一个非标准扩展,在 POSIX 中找不到。它在bash和 中可用ksh,因此使用可能很普遍。

Example

例子

cd /tmp
if [ -a test-file ] ; then
    echo 1: test-file exists
else
    echo 1: test-file missing
fi

touch test-file

if [ -a test-file ] ; then
    echo 2: test-file exists
else
    echo 2: test-file missing
fi

var=somerthing
if [ -n "$var" -a -a test-file ] ; then
    echo variable var is not empty and test-file exists
fi
rm -f test-file

回答by Cory Klein

-aLinks two expressions together in an "and" or "&&" expression. This option is deprecated.

-a在“and”或“&&”表达式中将两个表达式链接在一起。此选项已弃用

-nChecks if the length of a string is nonzero.

-n检查字符串的长度是否为非零。

You could translate the test expression into the following pseudocode:

您可以将测试表达式转换为以下伪代码:

if ( ( has nonzero length) and
     ( has nonzero length) and
     ( has nonzero length) )

There are no checks in that expression for whether the file exists or doesn't exist, only whether the arguments have been supplied to the script.

在该表达式中不检查文件是否存在,只检查参数是否已提供给脚本。

The arguments -aand -ncan be found in the manpage for test

参数-a-n可以在联机帮助页中找到test

man test

The operator [ ... ]is often used as shorthand for test ...and likely has identical functionality on your system.

运算符[ ... ]通常用作简写,test ...并且可能在您的系统上具有相同的功能。