- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这不是一个真正的问题(虽然我在最后提出了一个问题),而是我想分享的一个问题的解决方案,以防它对其他人有帮助。
很长一段时间以来,我在打开新终端时收到 bash: [: too many arguments
(特别是安装了 bash-completion macport 的 OS X 上的 iTerm2)。此错误源自行 if [ -n "$BASH_VERSION"-a -n "$PS1"-a -z "$BASH_COMPLETION_COMPAT_DIR"];然后在文件
。我终于找到了问题所在,因为我在我的 /opt/local/etc/bash_completion
中.bash_profile
中有 export PS1='>'
。将 PS1 更改为其他内容(例如 '> '
)可解决 bash 补全问题。
在 OS X 和 Debian 中进行的一些实验表明,在将额外的表达式(使用 -a
或 -o
)添加到测试 ([ ]
) 在涉及 '>'
的表达式之后。例如,
> A='>'; if [ -n "$A" ]; then echo "yes"; fi
yes
> A='>'; if [ -n "$A" -a -n "$A" ]; then echo "yes"; fi
bash: [: too many arguments
> A='> '; if [ -n "$A" -o -n "$A" ]; then echo "yes"; fi
yes
> A='>'; if [ -n "$A" -o -n "Hello" ]; then echo "yes"; fi
bash: [: too many arguments
> A='>'; if [ -n "Hello" -a -n "$A" ]; then echo "yes"; fi
yes
这是 bash 中的(已知)错误吗?
最佳答案
你的解决方法是有效的,只要字符串存储在$A
不是 [
的运算符/test
识别 - 正如您所发现的,只需添加一个空格就足够了。
Surely the "greater than" should be interpreted as just a string? It works with '> ' after all.
没有,$A
的内容不被解释为一个字符串。 (如果你想要那样,你必须改用 [[
,它在特殊的上下文中被解析,更像你对传统编程语言的期望。)
[
( test
) 是内置的(在大多数系统上也作为外部实用程序存在),因此使用命令语法 进行解析,这意味着:
$A
在这种情况下,引用将替换为变量的内容。[
因此,从[
的角度来看, 它最终看到的运算符是否无关紧要 - >
在您的示例中 - 来自文字或存储在变量中。
但请注意,空格很重要:传递 >
(无空格)被解释为运算符; >
,相比之下,><space>
不是 - 因为确切的文字 比运算符更多。
底线是:
-o
/ -a
为了避免您遇到的歧义(强调我的):The XSI extensions specifying the -a and -o binary primaries and the '(' and ')' operators have been marked obsolescent. (Many expressions using them are ambiguously defined by the grammar depending on the specific expressions being evaluated.)
具体来说,使用单独的 [ ... ]
用 &&
连接的表达式(而不是 -a
)和 ||
(而不是 -o
)解决了问题:
[ -n "$BASH_VERSION" ] && [ -n "$PS1" ] && [ -z "$BASH_COMPLETION_COMPAT_DIR" ]
或者,更简单地说,利用非空字符串的计算结果为真:
[ "$BASH_VERSION" ] && [ "$PS1" ] && [ -z "$BASH_COMPLETION_COMPAT_DIR" ]
注意虽然 -a
和 -o
引入歧义,它们不是安全问题 - 您不能通过使用它们来注入(inject)任意代码。
关于庆典 [ : too many arguments greater than symbol,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35578327/
到底是做什么的| bash - 这段代码第一行末尾的 do in a Dockerfile? 为什么 - 在末尾? RUN curl --silent --location https://rpm.n
当我运行这个 bash 脚本时: if [ [$EUID -ne 0] ]; then echo "This script must be run as root" 1>&2 exit 1
我有一个 bash 脚本: #!/bin/bash while : do nc -l -p 30003 | python3 script.py done 我希望聆听始终
执行命令 echo 'a b' 'c' 输出 a b c 但是下面的 X="'a b' 'c'" echo $X; 会出局 'a b' 'c' 我正在寻找一种取消引用 $X 的方法,这样它将输出 "a
我确实尝试在网上找到答案,但不幸的是空手而归。结合其他关键字搜索“./”会带来很多结果,但没有任何帮助... 无论如何,您可能会说,我对 MacOs 还是比较陌生。我正在从 shell 运行 mong
我正在使用具有以下语法的 bash 脚本 $ declare -a THIS[*] 这在 zsh 中似乎是非法的(我收到“未找到匹配项:THIS[*]”错误)。谁能帮我把它翻译成 zsh? 此外 -
我正在尝试暂停或“休眠”OSX 中运行的 bash 脚本中的特定行。该脚本在登录前启动时运行。我没有看到预期的结果。换句话说,无论我在“ sleep ”之后指定什么时间,脚本仍然会立即移动,不会有任何
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
#!/bin/bash jobname="job_201312161447_0003" jobname_pre=${jobname:0:16} jobname_post=${jobname:17} 这
#!/bin/bash jobname="job_201312161447_0003" jobname_pre=${jobname:0:16} jobname_post=${jobname:17} 这
#!/bin/bash jobname="job_201312161447_0003" jobname_pre=${jobname:0:16} jobname_post=${jobname:17} 这
我想制作自己的程序,但我不知道如何...例如,我想制作一个典型的“Hello $user”程序。 所以.. ├── hi │ ├── hi.sh │ ├── hi_to.sh hi.sh #!
我正在学习 Linux 中的 bash 脚本,我想解决一个我认为很容易但我就是想不通的问题。 我想插入数字作为参数,例如: sh script.sh 5 如果我插入 5 (1+2+3+4+5)=15,
Run a recursive listing of all the files in /var/log and redirect standard output to a file called l
这不是一个真正的问题(虽然我在最后提出了一个问题),而是我想分享的一个问题的解决方案,以防它对其他人有帮助。 很长一段时间以来,我在打开新终端时收到 bash: [: too many argumen
我想创建一个简单的 bash 脚本来在 OS X 上启动 Java 程序。文件名、文件路径和直接工作文件夹都包含空格。当我这样做时: #!/bin/sh cd `dirname $0` 我明白了 us
我正在关注 Discover Rust 的官方文档.我正在尝试将程序刷写到 STM32F3 中。 Flash chapter要求执行这个命令 -q target/thumbv7em-none-eab
如果我在 bash 脚本中使用 -e 选项并且代码失败 - 我可以捕获它: $ set -e; ( false; echo qwe; ) || echo qwe qwe 但是,当我尝试获取返回码时,我
我正在尝试从 64 位 Windows 上的 Cygwin 中编译一些 C 代码,并使用一些参数调用可执行文件。编译顺利进行(Makefile 在下面)。但是当我运行可执行文件时,出现错误 canno
我需要使用 ssh 在网络托管服务器(操作系统:Cent OS 6.4,perl v5.10.1)上安装几个 perl 模块。 在服务器上: cpan -i Template::Toolkit 报错:
我是一名优秀的程序员,十分优秀!