如果条件一次又一次不起作用,为什么相同?

时间:2019-09-13 13:35:23

标签: bash

嗨,Stackoverflow社区。

我有一个问题(可能是零星的)。

对于不相等的字符串,bash if条件产生一个结果,而另一次则产生错误消息“ [[:未找到命令”。

test.sh的示例代码:

#! /bin/bash

arg1=$1

echo -e "arg1: " $arg1

if  [[ $1 != "test" ]] && [[ $1 != "testing" ]]; then
        echo "test1"
fi
if  [[ $1 != "test" ]] && [[ $1 != "testing" ]]; then
        echo "test2"
fi

输出:

$ ./test.sh foo
arg1:  foo
test1
./test.sh: line 10:  [[: command not found

如您所见,第二个echo命令将永远不会到达。

我在MacOS bash(GNU bash版本3.2.57(1)-发行版(x86_64-apple-darwin18))和Linux bash(GNU bash版本4.2.46(2)-发行版(x86_64)上测试了此代码段-redhat-linux-gnu)),并且每次都可以确认该错误。

(我的最佳猜测是存在某种编码问题,但我还不能确定)。

在此先感谢您的帮助和/或指示。

1 个答案:

答案 0 :(得分:1)

感谢@gbajson和@ I0b0找到了原因:

我的错误是我在“ [[]”之前的第11行中包含了一个不间断空格(Oct 0240,Dec 160,Hex 0xA0,HTML)。