2

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!

6
  • 1
    Try echo "X${returnVar}X" to see if it contains white space? Commented Nov 27, 2013 at 12:33
  • Using echo "X${returnVar}X" give the output Line1: X Line2: Value-Enabled falseX Which means there's a new line character at the start maybe? Apologies for the bad format.. couldn't make it work properly :( Commented Nov 27, 2013 at 12:42
  • If I am right then echo "X${returnVar}X" would return two X's minimum, one before and one after your returnVar (at the line in the script where your echo is). The question was: Is there leading or trailing white space in returnVar? (It would be visible if sourrounded by X's.) Commented Nov 27, 2013 at 12:51
  • Yeah it looks like a new line character is the problem. How exactly would I use the tr command to trim a new line in this situation? Commented Nov 27, 2013 at 12:58
  • Try returnValue=$(tr '\n' ' ' <<< "$returnValue") before using returnValue. Alternatively, specify the new line in the comparison like if [ "$returnVar" == $'Value-Enabled\nfalse' ]. Commented Nov 27, 2013 at 13:32

3 Answers 3

4

I faced a similar issue, and the problem was that one of the strings was ending with the '\r' carriage return, and the other didn't.

So, you may want to sanitize the strings (remove any special characters) before the comparison. I was only able to catch the '\r' when I wrote a short python script to do the string comparison for me:

#!/usr/bin/env python
import sys
if  len(sys.argv) == 3 and \
    len(sys.argv[1]) > 0 and \
    len(sys.argv[2]) > 0 and \
    sys.argv[1].rstrip('\r') == sys.argv[2].rstrip('\r'):
    print "same"
else:
    print "different\n"-

In bash you can remove the '\r' from the $str string the following way:

str_sanitized=$(echo $str | tr -d '\r')
Sign up to request clarification or add additional context in comments.

1 Comment

Thanxx a lot.. I being a beginner was not being able to find out the issue. :) It worked for me..
2

Finally got it! This is the new code that works for me:

returnVar=$(/bin/sh ./returnVar.sh ${1})

newVar=$(echo "${returnVar}" | tr -d '\n')

if [ "${newVar}" == "Value-Enabled false" ] ;  then 
    variableName="variable1"
    #do other stuff here


elif [ "${newVar}" == "Value-Enabled true" ] ; then 
    variableName="variable2"
    #do other stuff here

else 
    echo error  
    exit 1
fi

saved the new value into variable called newVar, and added curly brackets around newVar in the if statements.

Comments

0

You can use the "-eq" operator instead of "==" which provides the required output.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.