我正在使用 SH shell,我试图将一个字符串与一个变量的值进行比较,但 if
条件总是执行为真。为什么?
这是一些代码:
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 或空字符串匹配吗?
最佳答案
您应该使用 =
运算符进行字符串比较:
Sourcesystem="ABC"
if [ "$Sourcesystem" = "XYZ" ]; then
echo "Sourcesystem Matched"
else
echo "Sourcesystem is NOT Matched $Sourcesystem"
fi;
man test
表示您使用 -z
来匹配空字符串。关于shell - 使用 sh shell 比较字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10849297/