4

我遗漏了一些关于 bash 的 if 构造/运算符或字符串比较的基本知识。考虑以下脚本:

#!/bin/bash
baseSystem="testdir1"
testme="NA"
if [ "$baseSystem"=="$testme" ]; then
    echo "In error case"
fi
if [ "$baseSystem"!="$testme" ]; then
    echo "In error case"
fi

我得到:

In error case
In error case

因此,即使它们应该互斥,它也会进入每个案例。任何帮助表示赞赏。

4

1 回答 1

8

bash恰好对空间有些特殊。

在运算符周围添加空格:

if [ "$baseSystem" == "$testme" ]; then

...

if [ "$baseSystem" != "$testme" ]; then

以下等价:

[ "$a"="$b" ]
[ "$a" = "$b" ]

您的第一个测试基本上与说if [ "testdir1==NA" ]; then哪个总是正确的相同。

于 2013-06-14T07:24:04.300 回答