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

查看:20
本文介绍了“[:参数太多"的含义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 open 方括号冒号参数太多.

上下文:单方括号中的 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: unary operator expected)

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天全站免登陆