我想将输出与字符串进行比较。我的脚本如下所示:
#!/bin/bash
DRBD=$(drbd-overview)
COMP=' 0:nfs-ha/0 Connected Secondary/Primary UpToDate/UpToDate'
DEBUG=1
if [ $DEBUG == "1" ];
then
echo "Debug:"
echo $COMP
echo $DRBD
echo "Debug-Ende"
fi
if [[ "x$DRBD" = "x$COMP" ]];
then
RTRN='Check_OK'
else
RTRN="$DRBD"
fi
echo "Return: $RTRN"
& bash -x script.sh 看起来像这样:
bash -x scripts.sh
++ drbd-overview
+ DRBD=' 0:nfs-ha/0 Connected Secondary/Primary UpToDate/UpToDate '
+ COMP=' 0:nfs-ha/0 Connected Secondary/Primary UpToDate/UpToDate'
+ DEBUG=1
+ '[' 1 == 1 ']'
+ echo Debug:
Debug:
+ echo 0:nfs-ha/0 Connected Secondary/Primary UpToDate/UpToDate
0:nfs-ha/0 Connected Secondary/Primary UpToDate/UpToDate
+ echo 0:nfs-ha/0 Connected Secondary/Primary UpToDate/UpToDate
0:nfs-ha/0 Connected Secondary/Primary UpToDate/UpToDate
+ echo Debug-Ende
Debug-Ende
+ [[ x 0:nfs-ha/0 Connected Secondary/Primary UpToDate/UpToDate = \x\ \0\:\n\f\s\-\h\a\/\0\ \ \C\o\n\n\e\c\t\e\d\ \S\e\c\o\n\d\a\r\y\/\P\r\i\m\a\r\y\ \U\p\T\o\D\a\t\e\/\U\p\T\o\D\a\t\e ]]
+ RTRN=' 0:nfs-ha/0 Connected Secondary/Primary UpToDate/UpToDate '
+ echo 'Return: 0:nfs-ha/0 Connected Secondary/Primary UpToDate/UpToDate '
Return: 0:nfs-ha/0 Connected Secondary/Primary UpToDate/UpToDate
我做错了什么 $COMP 在 bash 语句中被转义?
答案1
两个字符串不相同。$DRBD
末尾有一个空格,而$COMP
没有(您可以在跟踪输出中看到这一点)。这就是比较未设置RTRN
为 的原因Check_OK
。