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

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

问题描述

我找不到任何一个简单直接的资源拼写出的含义和解决以下BASH外壳的错误,所以我张贴了我研究后发现的。

错误:

  -bash:[:参数太多

谷歌的版本: 庆典开括号的参数太多

上下文:的条件,如果单括号用一个简单的比较操作符一样平等,不是等更大,例如:

 可变= $(/一些/命令);
如果[$变量== 0];然后
  #一些行动
科幻


解决方案

如果你的 $变量是包含空格或其他特殊字符,<一个字符串href=\"http://serverfault.com/questions/52034/what-is-the-difference-between-double-and-single-square-brackets-in-bash\">and单用方括号(这是测试命令的快捷方式),则该字符串可能拆分为多个单词。这些每一个都视为一个单独的参数。

这样的一个变量拆分为很多争论

同样会有任何函数调用放下含有空格或其他特殊字符的字符串真。


简易修复

裹在双引号中的可变输出,迫使它留作为一个字符串(因此一个参数)。例如,

 可变= $(/一些/命令);
如果[$变量== 0];然后
  #一些行动
科幻

就这么简单。但是,跳到也提防......下面,如果你也不能保证您的变量将不会是空字符串,或者一个字符串,其中包含无非是空白


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

本只存在在bash(显然Korn和zsh的),但是,因此可能无法与名为default弹/ bin / sh的等兼容这意味着,一些系统中,例如,它可能会从控制台的工作,但不从 cron的,取决于所有的配置方式。

这应该是这样的:

 可变= $(/一些/命令);
如果[[$变量== 0];然后
  #一些行动
科幻


也提防在 [组成:一元运算符预期错误

如果你看到了参数太多的错误,机会是你从非predictable输出函数得到一个字符串。 如果它也可能得到一个空字符串(或所有空白字符串),这将被视为即使有上面​​的速战速决零参数,并会失败, [ :一元运算符预期

这是同样的疑难杂症,如果你对使用其他语言 - 你不要指望一个变量的内容进行有效地印入code这个样子被评估之前

下面是一个例子,prevents两个 [:参数太多 [:一元运算符预期错误:更换具有缺省值的输出,如果它是空的(在这个例子中, 0 ),用双引号缠整个事情:

 可变= $(/一些/命令);
如果[$ {变量:-0}== 0];然后
  #一些行动
科幻

(这里,如果将$变量为0,或空发生的动作。当然,你应该在0(默认值)更改为不同的默认值,如果不同的行为而被通缉)


最后说明:由于 [测试,上述所有的快捷方式也是错误真正的测试:参数太多(也是测试:一元运算符预期

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.

The error:

-bash: [: too many arguments

Google-friendly version: bash open square bracket too many arguments.

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 

解决方案

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.

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


Easy fix

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

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.

It would look like this:

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

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.

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 

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

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

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