I have a bash script that calls another bash script and gets a string return value from that second script. I then try to compare that string to a hardcoded value and, even though I think it's right, it's skipping the if statement and going to the else.
Here's some code, just changed variable names:
returnVar=$(/bin/sh ./returnVar.sh ${1})
echo variable from second script = $returnVar
if [ "$returnVar" == "Value-Enabled false" ] ; then
variableName="variable1"
#do other stuff here
elif [ "$returnVar" == "Value-Enabled true" ] ; then
variableName="variable2"
#do other stuff here
else
echo error
exit 1
fi
This staement:
echo variable from second script = "$returnVar"
seems to return the answer I want, "Value-Enabled false", but the comparison is not working. Can anyone see where I'm going wrong and know how to help?
I have also tried hardcoding the returnVar as the string I want, which then works! So something's going wrong that I don't know about.
Thanks in advance!
echo "X${returnVar}X"to see if it contains white space?echo "X${returnVar}X"would return two X's minimum, one before and one after yourreturnVar(at the line in the script where your echo is). The question was: Is there leading or trailing white space inreturnVar? (It would be visible if sourrounded by X's.)returnValue=$(tr '\n' ' ' <<< "$returnValue")before using returnValue. Alternatively, specify the new line in the comparison likeif [ "$returnVar" == $'Value-Enabled\nfalse' ].