bash string comparar

0

Gostaria de comparar uma saída com uma string. Meu script é assim:

#!/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 é assim:

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\ 
#!/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"
\:\n\f\s\-\h\a\/
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\ %pre%\:\n\f\s\-\h\a\/%pre%\ \ \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
\ \ \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

o que eu fiz de errado que $ COMP é escapado dentro da declaração bash?

    
por Nico Wunder 02.02.2017 / 13:40

1 resposta

1

As duas cadeias não são idênticas. $DRBD tem um espaço no final, enquanto $COMP não (você pode ver isso na saída de rastreio). É por isso que a comparação não define RTRN para Check_OK .

    
por 02.02.2017 / 13:52

Tags