0

I am trying to compare two strings in an if statement. However the comparison doesn't seem to be operating correctly as the program passes the first condition every time.

#!/bin/bash
switch=$(ssh 192.168.14.10 egrep somefile /tmp/tmpfile.txt)
if [ "$switch" == "tmp" ]; then
     echo "expected output"
else
     echo "unexpected output"
fi

The output goes to "expected output" every time. I've even tried changing the first condition to be "!=" but it arrives to the first statement still.

  • 1
    Try replacing == with a single = – doneal24 Dec 6 '16 at 0:13
  • Isn't single "=" used for assignment, not comparison? – keyboard_solo Dec 6 '16 at 0:23
  • @keyboard_solo, no, a single = with no spaces around it means variable assignment if it's in the context of a "simple command" (basically if it's the first "token" on a line). The single = in this case is just a text argument, nothing special, and is passed to the [ command. (Yes, [ is a command, not part of shell syntax. It's also known as the test command.) – Wildcard Dec 6 '16 at 1:13
  • Is that script written exactly as you are using it? If so, your ssh address is horribly out of range. – Timothy Martin Dec 6 '16 at 1:45
0

Your comparison is correct and idiomatic Bash code.

Try inserting echo "switch='$switch'" before the if to see what the variable contents are each time.

  • Yes. I've tried that... I'm really not sure what's wrong at this point. The weird thing is that when I change the first statement with the "==" to "!=", I get the first condition either way. The output is "expected output". But when I change the text of the first statement "tmp" to "abctmp" while its "==", then the condition goes to the second statement, yielding "unexpected output" – keyboard_solo Dec 6 '16 at 0:30
0

I would start by ruling out spurious spaces:

if [ "$(printf "%s" "$switch"| tr -d " ")" = tmp ]; then

If still unsuccessful, add both of these lines before the test. If their output is different, fix your test.

echo tmp | od -c
echo "$switch" | od -c
  • Good point. I made sure to remove spaces by doing 'pipe' 'sed'. I echoed both items being compared before the if statement so I can manually check. I'm not really sure what the issue is at this point. Thanks for the pointers. – keyboard_solo Dec 8 '16 at 18:44
0

Change the first line from:

switch=$(ssh 192.16810.10.10 egrep somefile /tmp/tmpfile.txt)

To the following:

export switch=$(ssh 192.16810.10.10 egrep somefile /tmp/tmpfile.txt)

I've encountered a near identical problem many times before, or when when incrementing a variable. Where variable=$variable++ or variable=$variable+1 would append the text string "+1" or "++" to the end each time it looped instead of incrementing like it like it should.

Using let variable=$variable++ or let variable=$variable+1 immediately fixed the issue. Using export also worked if everything was set before the loop started.

I believe bash is doing something wrong when it stores that variable. Thus it makes the comparison act up, and using let/export does something different to store it. I was searching for an answer to the same question, except I was getting inconsistent results comparing numbers instead of strings. In any case, it can't hurt to try using export before the first statement.

  • Sorry for editing that post so many times. But, I typed the wrong – TheNH813 Dec 6 '16 at 1:52
  • thing to use for text variables. Also sorry for double commenting, as I'm new here and didn't know you couldn't press enter in comments. – TheNH813 Dec 6 '16 at 1:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.