本文介绍了巴什if语句行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我失去了一些东西根本就不是在bash的if结构/经营者或字符串比较。
请看下面的代码:

I am missing something fundamental concerning either the bash's if construct/operators or string comparison.Consider the following script:

#!/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

因此​​,进入每一种情况下,即使它们应该mututally排斥。
任何帮助是AP preciated。

So it enters each case even though they should be mututally exclusive.Any help is appreciated.

推荐答案

庆典恰好是有点讲究的空间。

bash happens to be somewhat particular about spaces.

添加空格围绕运营商:

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

...

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

以下是不会等价的:

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

您第一次测试是基本相同的话说如果[testdir1 == NA];然后这将永远是正确的。

Your first test is essentially the same as saying if [ "testdir1==NA" ]; then which would always be true.

这篇关于巴什if语句行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 02:13