1

I'm having trouble comparing two strings within an if statement using double brackets. Here is what I have below. The $statusID variable is pulled from a file elsewhere and will pull either exactly "HELLO" or "WORLD" but when used below the string doesn't match and ends up using the else statement instead.

 if [[ "$statusID" == "HELLO" ]]; then
     #DO SOMETHING HERE
 elif [[ "$statusID" == "WORLD" ]]; then
     #DO SOMETHING ELSE
 else
     #DO SOMETHING ELSE AGAIN
 fi

I've tried using wildcards which will work but causes problems when I have a similar variable later such as "HIHELLO" it runs the "HELLO" if statement instead of "HIHELLO". I don't want to use this method because of that problem.

 if [[ "$statusID" == *"HELLO"* ]]; then
     #DO SOMETHING HERE
 elif [[ "$statusID" == *"WORLD"* ]]; then
     #DO SOMETHING ELSE
 else
     #DO SOMETHING ELSE AGAIN
 fi

I'm trying to compare the literal value of the strings. If the value of $statusID is HELLO I want the if statement to be true and run that code. I've looked at other similar posts on stack overflow and a few other sites but I'm not understanding what I'm missing. Is it how I'm pulling the variable? Is it how I'm comparing them?

I will note that if I set the variable directly such as:

 statusID="HELLO" or statusID=HELLO

within terminal directly (as opposed to running a script) it seems to match properly.

Inian
  • 80,270
  • 14
  • 142
  • 161
  • 3
    `statusID` variable is pulled from a file? Could you `echo` the contents? or possible do a `hexdump`? i.e. `echo "$statusID" | hexdump -c` – Inian Sep 24 '18 at 14:17
  • I'm pulling it in this manner: `statusID=$(cat "$ref" | tr "," "\\t" | grep "$serialNumber" | awk '{print $3}')` from a reference file that searches by serial number. – joshuaspatrick Sep 24 '18 at 14:20
  • Then definitely print it out using hexdump, or with delimiters, eg `echo ##${statusID}##` to make sure it is exactly what you are expecting. – Gem Taylor Sep 24 '18 at 14:25
  • 1
    Again, though, what is the value at this point when the comparison occurs. Your syntax is correct (although the extra quotes around the variable are superfluous, but not harmful... I don't think anyway) so our next best guess is that the value of your variable isn't what you think it is. Perhaps an extra space is hanging out unexpectedly? – JNevill Sep 24 '18 at 14:25
  • It could be DOS line endings in either your script or an input file. There's no way to tell without seeing the *exact* value of `$statusID`, but it's clear that it's not the value you think it has. – chepner Sep 24 '18 at 14:33
  • Your command to set `statusID` is unnecessarily complicated: `statusID=$(awk -F, -v sn="$serialNumber" '$0 ~ sn {print $3}')` should suffice. – chepner Sep 24 '18 at 14:35
  • All of you are correct. I just tried the hexdump Inian suggested. It is printing out extra characters when pulling from the reference file. Is there an easy way for me to incorporate a fix for that so it doesn't print this additional characters? Hexdump is printing the following: `echo "$statusID" | hexdump -c 0000000 F I X D A A S \r \n 0000009` – joshuaspatrick Sep 24 '18 at 14:36

0 Answers0