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

查看:153
本文介绍了“[:太多参数”的含义] 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条件,带有一个简单的比较运算符,如equals,大于等,例如:

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 是包含空格或其他特殊字符的字符串, - >和单方括号(这是测试的快捷方式命令),然后字符串可以分成多个单词。其中每一个都被视为一个单独的参数。

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.

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

So that one variable is split out into many arguments:

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.

或者,备用修复是使用双方括号(这是新测试的快捷方式命令)。

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不兼容等。这意味着在某些系统上,例如,它可能来自控制台但不能来自 cron ,具体取决于所有内容的配置。

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. This means on some systems, for example, it might work from the console but not from cron, depending on how everything is configured.

看起来像这样:

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






还要注意 [:一元运算符预期错误



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


Also beware of the [: unary operator expected error

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

如果你习惯了其他语言,那就是'gotcha' - 你不期望它的内容变量要在评估之前有效地打印到代码中。

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.

这是一个阻止 [:太多参数 [:一元运算符预期错误:如果输出为空,则将输出替换为默认值(在此示例中, 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)

最终注释:由于 [测试的快捷方式 ,以上所有错误测试也是如此:参数太多(还有测试:一元运算符预期

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