在bash`if [..]`语句中检查命令是否成功 [英] Checking the success of a command in a bash `if [ .. ]` statement
问题描述
我正在尝试使我们的应用程序备份自动化.该过程的一部分是在if
语句中检查egrep
的退出状态:
I am trying to automate our application backup. Part of the process is to check the exit status of egrep
in an if
statement:
if [ ! -f /opt/apps/SiteScope_backup/sitescope_configuration.zip ] ||
[ egrep -i -q "error|warning|fatal|missing|critical" "$File" ]
then
echo "testing"
fi
我希望它输出testing
,因为该文件存在并且egrep
返回成功,但是却出现了错误:
I expected it to output testing
because the file exists and egrep
returns success, but instead I'm getting an error:
-bash: [: too many arguments
我尝试了各种语法-其他括号,引号等,但错误仍然存在.
I tried with all kinds of syntax - additional brackets, quotes etc but error still persists.
请帮助我理解我要去哪里.
Please help me in understanding where I am going wrong.
推荐答案
您会犯一个普遍的错误,即假定[
是if
语句的语法的一部分.它不是; if
的语法很简单
You are making the common mistake of assuming that [
is part of the if
statement's syntax. It is not; the syntax of if
is simply
if command; then
: # ... things which should happen if command's result code was 0
else
: # ... things which should happen otherwise
fi
我们使用的常见的command
之一是[
,它是命令test
的别名.这是比较字符串,数字和文件的简单命令.它接受参数的相当狭窄的组合,并且如果您不传递期望的参数,则往往会产生令人困惑和误导性的错误消息. (或者更确切地说,一旦您习惯了这些错误消息,它们就足够了并且很有帮助,但是如果您不习惯的话,它们很容易被误解.)
One of the common command
s we use is [
which is an alias for the command test
. It is a simple command for comparing strings, numbers, and files. It accepts a fairly narrow combination of arguments, and tends to generate confusing and misleading error messages if you don't pass it the expected arguments. (Or rather, the error messages are adequate and helpful once you get used to it, but they are easily misunderstood if you're not used.)
在这里,您要检查命令egrep
的结果:
Here, you want to check the result of the command egrep
:
if [ ! -f /opt/apps/SiteScope_backup/sitescope_configuration.zip ] ||
egrep -i -q "error|warning|fatal|missing|critical" "$File"
then
echo "testing"
fi
在通常情况下,command
可以是管道或命令列表;那么,最终命令的退出代码就是if
将要检查的状态,类似于脚本中的最后一个命令如何确定脚本的退出状态.
In the general case, command
can be a pipeline or a list of commands; then, the exit code from the final command is the status which if
will examine, similarly to how the last command in a script decides the exit status from the script.
这些复合命令可以任意复杂,例如
These compound commands can be arbitrarily complex, like
if read thing
case $thing in
'' | 'quit') false;;
*) true;;
esac
then ...
但是在实践中,您很少在if
语句中看到多个命令(尽管这并非闻所未闻;您将||
与复合语句一起使用就是一个很好的例子!)
but in practice, you rarely see more than a single command in the if
statement (though it's not unheard of; your compound statement with ||
is a good example!)
只需说明一下,
if [ egrep foo bar ]
在参数egrep foo bar
上运行[
aka test
.但是不带选项的[
仅接受一个参数,然后检查该参数是否为空字符串. (egrep
显然不是一个空字符串.这里的引号是可选的,但也许会更容易看到:
is running [
aka test
on the arguments egrep foo bar
. But [
without options only accepts a single argument, and then checks whether or not that argument is the empty string. (egrep
is clearly not an empty string. Quoting here is optional, but would perhaps make it easier to see:
if [ "egrep" ]; then
echo "yes, 'egrep' is not equal to ''"
fi
这显然是孤立的愚蠢,但希望可以作为一个示例.)
This is obviously silly in isolation, but should hopefully work as an illustrative example.)
test
作为一般厨房水槽的历史原因(作者不想成为if
的语法的一部分)是原始Bourne外壳吸引力较差的设计之一. Bash和zsh
提供了不太麻烦的替代方法(例如bash中的[[
双括号),当然,POSIX test
比Bell Labs的原始版本脾气好得多.
The historical reasons for test
as a general kitchen sink of stuff the authors didn't want to make part of the syntax of if
is one of the less attractive designs of the original Bourne shell. Bash and zsh
offer alternatives which are less unwieldy (like the [[
double brackets in bash), and of course, POSIX test
is a lot more well-tempered than the original creation from Bell Labs.
这篇关于在bash`if [..]`语句中检查命令是否成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!