庆典 - if语句 - [:参数太多 [英] bash - if statement - [: too many arguments

查看:709
本文介绍了庆典 - if语句 - [:参数太多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我们的自动化应用程序备份。整个code的一部分是一个if语句或条件被投掷 - [:参数太多错误。下面是if语句。

I am trying to automate our application backup. Part of the entire code is a if statement with OR condition which is throwing a - [: too many arguments error. Below is the if statement.

if [ ! -f /opt/apps/SiteScope_backup/sitescope_configuration.zip ] ||
   [ egrep -i -q "error|warning|fatal|missing|critical" "$File" ]
then 
  echo "testing"
fi
-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.

推荐答案

您正在承担的常见的错误, [是一部分的如果语句的语法。不是这样;如果的的语法很简单:

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

一个常见的​​命令取值,我们使用 [这是命令<$ C $别名C>测试。它是对比较字符串,数字和文件的简单命令。它接受的参数一个相当狭窄的结合,容易产生混淆和误导的错误消息,如果你不把它传递预期的参数。 (或者说,该错误信息是充分的和有益的,一旦你习惯了它,但他们,如果你不使用容易被误解。)

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

在一般情况下,命令可以是管道或命令列表;然后,从最终命令的退出code是它如果将检查状态,类似于在脚本的最后一个命令如何决定从脚本退出状态。

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 ...

但在实践中,很少看到比如果语句一个命令的更多(尽管它不是闻所未闻的;你的复合语句与 | | 是一个很好的例子)

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!)

历史原因测试的东西一般厨房水槽的作者并不想如果的语法的一部分code>是原来的Bourne shell的不那么有吸引力的设计之一。 Bash和的zsh 提供替代这是不太笨重(如 [在bash双括号),当然, POSIX 测试是一个很大的好脾气不是从贝尔实验室原创。

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.

这篇关于庆典 - if语句 - [:参数太多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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