我使用SH shell,我试图比较一个字符串与变量的值,但if条件总是执行为true。为什么?
这里是一些代码:
Sourcesystem="ABC" if [ "$Sourcesystem" -eq 'XYZ' ]; then echo "Sourcesystem Matched" else echo "Sourcesystem is NOT Matched $Sourcesystem" fi; echo Sourcesystem Value is $Sourcesystem ;
即使这不工作:
Sourcesystem="ABC" if [ 'XYZ' -eq "$Sourcesystem" ]; then echo "Sourcesystem Matched" else echo "Sourcesystem is NOT Matched $Sourcesystem" fi; echo Sourcesystem Value is $Sourcesystem ;
第二,我们可以将其与NULL或空字符串匹配吗?
您应该使用=运算符进行字符串比较:
原文链接:https://www.f2er.com/bash/392353.htmlSourcesystem="ABC" if [ "$Sourcesystem" = "XYZ" ]; then echo "Sourcesystem Matched" else echo "Sourcesystem is NOT Matched $Sourcesystem" fi;
man测试说,你使用-z匹配空字符串。