“[:参数太多"的含义来自 if [] 的错误(方括号) [英] Meaning of "[: too many arguments" error from if [] (square brackets)

查看:32
本文介绍了“[:参数太多"的含义来自 if [] 的错误(方括号)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到任何简单明了的资源来说明以下 BASH shell 错误的含义并修复它,因此我将在研究后发布我发现的内容.

I couldn't find any one simple straightforward resource spelling out the meaning of and fix for the following BASH shell error, so I'm posting what I found after researching it.

错误:

-bash: [: too many arguments

Google 友好版本: bash 打开方括号冒号太多参数.

上下文:在单个方括号中的 if 条件,带有简单的比较运算符,如等于、大于等,例如:

Context: an if condition in single square brackets with a simple comparison operator like equals, greater than etc, for example:

VARIABLE=$(/some/command);
if [ $VARIABLE == 0 ]; then
  # some action
fi 

推荐答案

如果您的 $VARIABLE 是包含空格或其他特殊字符的字符串,使用单方括号(这是的快捷方式test 命令),则字符串可能被拆分为多个单词.这些中的每一个都被视为一个单独的参数.

If your $VARIABLE is a string containing spaces or other special characters, and single square brackets are used (which is a shortcut for the test command), then the string may be split out into multiple words. Each of these is treated as a separate argument.

这样一个变量被拆分成多个参数:

VARIABLE=$(/some/command);  
# returns "hello world"

if [ $VARIABLE == 0 ]; then
  # fails as if you wrote:
  # if [ hello world == 0 ]
fi 

对于任何放下包含空格或其他特殊字符的字符串的函数调用,同样如此.

The same will be true for any function call that puts down a string containing spaces or other special characters.

将变量输出用双引号括起来,强制将其保留为一个字符串(因此是一个参数).例如,

Wrap the variable output in double quotes, forcing it to stay as one string (therefore one argument). For example,

VARIABLE=$(/some/command);
if [ "$VARIABLE" == 0 ]; then
  # some action
fi 

就这么简单.但是如果你也不能保证你的变量不是空字符串,或者是一个只包含空格的字符串,请跳到下面的也当心...".

Simple as that. But skip to "Also beware..." below if you also can't guarantee your variable won't be an empty string, or a string that contains nothing but whitespace.

或者,一个替代修复是使用双方括号(这是new test命令的快捷方式).

Or, an alternate fix is to use double square brackets (which is a shortcut for the new test command).

这仅存在于 bash(显然 korn 和 zsh)中,因此可能与 /bin/sh 等调用的默认 shell 不兼容.

This exists only in bash (and apparently korn and zsh) however, and so may not be compatible with default shells called by /bin/sh etc.

这意味着在某些系统上,它可能在控制台中工作,但在其他地方调用时可能无法工作,例如从 cron 调用,这取决于一切的配置方式.

This means on some systems, it might work from the console but not when called elsewhere, like from cron, depending on how everything is configured.

它看起来像这样:

VARIABLE=$(/some/command);
if [[ $VARIABLE == 0 ]]; then
  # some action
fi 

如果您的命令包含这样的双方括号,并且您在日志中收到错误,但它可以从控制台运行,请尝试将 [[ 换成此处建议的替代方法,或者确保任何运行您的脚本使用支持 [[ aka new test.

If your command contains double square brackets like this and you get errors in logs but it works from the console, try swapping out the [[ for an alternative suggested here, or, ensure that whatever runs your script uses a shell that supports [[ aka new test.

如果您看到参数过多"错误,很可能是您从具有不可预测输出的函数中获取字符串.如果也有可能得到一个空字符串(或所有空白字符串),即使使用上述快速修复",这也将被视为零参数,并且会因 [: 一元运算符而失败预期

If you're seeing the "too many arguments" error, chances are you're getting a string from a function with unpredictable output. If it's also possible to get an empty string (or all whitespace string), this would be treated as zero arguments even with the above "quick fix", and would fail with [: unary operator expected

如果您习惯于其他语言,这也是同样的问题"——您不希望变量的内容在评估之前像这样有效地打印到代码中.

It's the same 'gotcha' if you're used to other languages - you don't expect the contents of a variable to be effectively printed into the code like this before it is evaluated.

这是一个防止 [: too many arguments[: unary operator expected 错误的示例:如果输出为空,则将其替换为默认值 (在这个例子中,0),用双引号包裹整个内容:

Here's an example that prevents both the [: too many arguments and the [: unary operator expected errors: replacing the output with a default value if it is empty (in this example, 0), with double quotes wrapped around the whole thing:

VARIABLE=$(/some/command);
if [ "${VARIABLE:-0}" == 0 ]; then
  # some action
fi 

(此处,如果 $VARIABLE 为 0 或为空,则操作将发生.当然,如果需要不同的行为,您应该将 0(默认值)更改为不同的默认值)

(here, the action will happen if $VARIABLE is 0, or empty. Naturally, you should change the 0 (the default value) to a different default value if different behaviour is wanted)

最后一点:由于[test的快捷方式,所以上述所有对于错误test: too也是成立的许多参数(还有test:一元运算符预期)

Final note: Since [ is a shortcut for test, all the above is also true for the error test: too many arguments (and also test: unary operator expected)

这篇关于“[:参数太多"的含义来自 if [] 的错误(方括号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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