在bash`if [..]`语句中检查命令是否成功 [英] Checking the success of a command in a bash `if [ .. ]` statement

查看:154
本文介绍了在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 commands 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆