Bash if语句行为

时间:2013-06-14 07:20:45

标签: bash

我遗漏了关于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

所以它进入每个案例,即使它们应该是互相排斥的。 任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:8)

bash恰好与空间有点特别。

在运营商周围添加空格:

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

...

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

以下等效:

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

你的第一次测试基本上与说if [ "testdir1==NA" ]; then完全一样。

相关问题