在 bash `if [ .. ]` 语句中检查命令是否成功
我正在尝试自动化我们的应用程序备份.部分过程是在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
很多比贝尔实验室的原创作品更温和.
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.