为什么会出现“找不到命令"的提示?与Bash条件中的NOT运算符一起使用? [英] Why do I get "command not found" with the NOT operator in a Bash conditional?

查看:114
本文介绍了为什么会出现“找不到命令"的提示?与Bash条件中的NOT运算符一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有

if [ ! -e $dir ];
then
 mkdir $dir
fi 

工作,但不

[[ ! -e $dir ]] || mkdir $dir 

为什么?

有了[[ ...我得到

 line 34: [[ !: command not found

编辑1

bash --version
GNU bash, version 3.2.25(1)-release (i686-redhat-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.

编辑2

在某些情况下有效而在某些情况下则无效,两个连续的命令

Edit 2

in some case work and some case don't work, two consecutives commands

[user@host ~]$ [ -e /tmp ] && date
-bash: [: missing `]'
[user@host ~]$ [ -e /tmp ] && date
mar jun 26 10:05:50 CLT 2012

推荐答案

特别是在编辑2之后,在我看来,这确实像是一个有趣"字符的问题,即非打印字符混入或看起来正常,但是-诸如不间断空格之类的怪异字符.这些可能很难发现.在编辑器中,一切看起来可能完全正常,即使您使用cat -v之类的内容查看脚本,也不会总是清晰地显示有趣的字符.如果您的系统上有xxd,这是一种精确查看文件内容的好方法.这是此类问题的快速演示:

Especially after edit 2, this really looks to me like a problem with "funny" characters, either nonprinting characters getting mixed in, or normal-looking-but-weird characters like nonbreaking spaces. These can be fairly hard to detect; things can look completely normal in an editor, and even if you view a script with something like cat -v it won't always show funny characters clearly. If you have xxd on your system, it's a really good way to see precisely what's in the file. Here's a quick demo of this type of problem:

$ cat -v nbsptest 
#!/bin/bash -x
[ -e /tmp ] && date
[ -e /tmp ] && date
[ -e /tmp ] && date
$ ./nbsptest 
+ '[ -e' /tmp ']'
./nbsptest: line 2: [ -e: command not found
+ '[' -e '/tmp ]'
./nbsptest: line 3: [: missing `]'
+ '[' -e /tmp ']'
+ date
Sat Jun 30 10:53:56 PDT 2012
$ xxd nbsptest 
0000000: 2321 2f62 696e 2f62 6173 6820 2d78 0a5b  #!/bin/bash -x.[
0000010: c2a0 2d65 202f 746d 7020 5d20 2626 2064  ..-e /tmp ] && d
0000020: 6174 650a 5b20 2d65 202f 746d 70c2 a05d  ate.[ -e /tmp..]
0000030: 2026 2620 6461 7465 0a5b 202d 6520 2f74   && date.[ -e /t
0000040: 6d70 205d 2026 2620 6461 7465 0a         mp ] && date.

使用cat -v(和morevi等),该脚本看起来完全正常,但是前两个命令失败. xxd显示原因:第一个命令在[-e之间具有UTF-8不间断空格(在十六进制列表中显示为c2a0,在文本列表中显示为[..-e),第二个命令具有以下内容: /tmp](在文本列表中为/tmp..])之间的不间断空格.

The script looks completely normal with cat -v (and more, vi, etc), but the first two commands fail. xxd shows why: the first command has a UTF-8 nonbreaking space between the [ and the -e (this shows as c2a0 in the hex listing, [..-e in the text listing) and the second command has a nonbreaking space between /tmp and ] (/tmp..] in the text listing).

-x显示屏(我使用了bash -x来调用它,也可以按照@CodeGnome的建议使用set -x),也提示了正在发生的事情.对于第一个命令,它将其列为'[ -e' /tmp ']' -请注意[ -e周围的引号,这表明外壳程序将所有内容都视为一个单词",这意味着它并不认为中间存在空格它的.同样,第二个命令显示为'[' -e '/tmp ]',并带有引号表示它认为/tmp ]都是一个单词".

The -x display (I used bash -x to invoke it, you can also use set -x as @CodeGnome suggested) also gives a hint about what's going on. For the first command, it listed it as '[ -e' /tmp ']' -- note the quotes around [ -e, which indicates that the shell is treating that all as one "word", which means it doesn't think that's a space in the middle of it. Similarly, the second command is displayed as '[' -e '/tmp ]' with the quotes indicating that it thinks /tmp ] is all one "word".

这篇关于为什么会出现“找不到命令"的提示?与Bash条件中的NOT运算符一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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