-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
What do the -n and -a options do in a bash if statement?
提问by surfi
采纳答案by sorpigal
Nitpicking
吹毛求疵
The switches -a
and -n
are not strictly part of a bash
if
statement in that the if
command does not process these switches.
开关-a
和-n
严格来说不是bash
if
语句的一部分,因为该if
命令不处理这些开关。
What are primaries?
什么是初选?
I call them "switches", but the bash
documentation 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 sh
scripts if
is a command that takes a command as its argument, executes it and tests its return code. If the return code is 0
the block of code following then
is executed up until the closing fi
or (if supplied) the following else
. If the return code was not 0
and an else
statement was supplied then the block of code following else
is executed up until the closing fi
.
在sh
脚本中if
是一个命令,它将命令作为参数,执行它并测试它的返回码。如果返回代码是0
以下代码块,then
则执行直到关闭fi
或(如果提供)以下代码else
。如果没有返回代码0
并且else
提供了一条语句,else
则执行下面的代码块直到结束fi
。
You can see this effect by passing if
the command true
or the command false
, which are simple commands that do nothing and return 0
and non-0
respectively.
你可以通过传递if
commandtrue
或 command来看到这种效果false
,它们是简单的命令,什么都不做0
,0
分别返回和非。
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 if
is [
, which is also sometimes known as test
. It is this command which takes the switches you're asking about. In bash
the test
command will be a built-in command; try type [
to learn its type. For built-in commands help
will show usage, so also run help [
to see documentation. Your system probably also has a /bin/[
and a /bin/test
and if you man test
you can see the manuals for those. Although the behavior of the built-in test
may 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.
在示例代码中,您提供了要传递给if
is的命令,[
有时也称为test
. 正是这个命令采用了您所询问的开关。在bash
该test
命令将是一个内置的命令; 尝试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 test
we can consult help test
or man test
and read its usage. This will show that-n
tests the following argument and evaluates to true if it is not an empty string.
知道您正在运行的命令是test
我们可以查阅help test
或man test
阅读其用法。这将显示-n
测试以下参数,如果它不是空字符串,则计算结果为 true。
In the documentation of test
you 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 -f
switch 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 -a
is 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 -e
switch. However, when -a
is 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 -a
is a non-standard extension which won't be foundin POSIX. It is available in bash
and 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
-a
Links two expressions together in an "and" or "&&" expression. This option is deprecated.
-a
在“and”或“&&”表达式中将两个表达式链接在一起。此选项已弃用。
-n
Checks 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 -a
and -n
can 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 ...
并且可能在您的系统上具有相同的功能。