Bash 以命令退出代码为条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18318560/
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
Bash conditional on command exit code
提问by Martin C. Martin
In bash, I want to say "if a file doesn't contain XYZ, then" do a bunch of things. The most natural way to transpose this into code is something like:
在 bash 中,我想说“如果文件不包含 XYZ,那么”做一堆事情。将其转换为代码的最自然的方法是:
if [ ! grep --quiet XYZ "$MyFile" ] ; then
... do things ...
fi
But of course, that's not valid Bash syntax. I could use backticks, but then I'll be testing the output of the file. The two alternatives I can think of are:
但当然,这不是有效的 Bash 语法。我可以使用反引号,但随后我将测试文件的输出。我能想到的两种选择是:
grep --quiet XYZ "$MyFile"
if [ $? -ne 0 ]; then
... do things ...
fi
And
和
grep --quiet XYZ "$MyFile" ||
( ... do things ...
)
I kind of prefer the second one, it's more Lispy and the || for control flow isn't that uncommon in scripting languages. I can see arguments for the first one too, although when the person reads the first line, they don't know whyyou're executing grep, it looks like you're executing it for it's main effect, rather than just to control a branch in script.
我更喜欢第二个,它更像是 Lispy 和 || 控制流在脚本语言中并不少见。我也可以看到第一个的参数,虽然当这个人阅读第一行时,他们不知道你为什么要执行 grep,看起来你正在执行它是为了它的主要效果,而不仅仅是为了控制一个脚本中的分支。
Is there a third, more direct way which uses an if
statement and has the grep
in the condition?
是否有第三种更直接的方式使用if
语句并具有grep
in 条件?
回答by user000001
Yes there is:
就在这里:
if grep --quiet .....
then
# If grep finds something
fi
or if the grep fails
或者如果 grep 失败
if ! grep --quiet .....
then
# If grep doesn't find something
fi
回答by Costi Ciudatu
You don't need the [
]
(test
) to check the return value of a command. Just try:
您不需要[
]
( test
) 来检查命令的返回值。你试一试:
if ! grep --quiet XYZ "$MyFile" ; then
回答by Dr. Jan-Philip Gehrcke
This is a matter of taste since there obviously are multiple working solutions. When I deal with a problem like this, I usually apply wc -l
after grep in order to count the lines that match. Then you have a single integer number that you can evaluate within a test condition. If the question only is whether there is a match at all (the number of matching lines does not matter), then applying wc
probably is OTT and evaluation of grep
's return code seems to be the best solution:
这是一个品味问题,因为显然有多种可行的解决方案。当我处理这样的问题时,我通常wc -l
在grep之后应用,以便计算匹配的行数。然后您就有了一个可以在测试条件下评估的整数。如果问题只是是否完全匹配(匹配行的数量无关紧要),那么应用wc
可能是 OTT 并且评估grep
的返回码似乎是最好的解决方案:
Normally, the exit status is 0 if selected lines are found and 1 otherwise. But the exit status is 2 if an error occurred, unless the -q or --quiet or --silent option is used and a selected line is found. Note, however, that POSIX only mandates, for programs such as grep, cmp, and diff, that the exit status in case of error be greater than 1; it is therefore advisable, for the sake of portability, to use logic that tests for this general condition instead of strict equality with 2.
通常,如果找到选定的行,退出状态为 0,否则为 1。但如果发生错误,退出状态为 2,除非使用 -q 或 --quiet 或 --silent 选项并找到选定的行。但是请注意,POSIX 仅要求对于 grep、cmp 和 diff 等程序,出现错误时的退出状态大于 1;因此,为了可移植性,建议使用测试此一般条件的逻辑,而不是与 2 严格相等。