LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   if statement fails when it shouldn't (https://www.linuxquestions.org/questions/linux-newbie-8/if-statement-fails-when-it-shouldnt-4175536073/)

aristosv 03-07-2015 11:03 PM

if statement fails when it shouldn't
 
For some reason this statement reports "FALSE" when in fact it shouldn't. Any idea why?

Code:

#!/bin/bash

var1=$(nc -dvzw5 thebes.openshells.net 22)
var2='Connection to thebes.openshells.net 22 port [tcp/ssh] succeeded!'
if [ "$var1" = "$var2" ]; then
echo TRUE
else
echo FALSE
fi


veerain 03-07-2015 11:08 PM

Quote:

if [ "$var1" = "$var2" ]; then
You try this:

Code:

if [ "$var1" == "$var2" ]; then
'=' is assignment operator. '==' is string equality operator.

aristosv 03-08-2015 12:07 AM

nope, it still reports FALSE :-/

grail 03-08-2015 07:25 AM

I would be curious why you think the two should be equal.

Place set -xv as thew second line in the script and check the output.

Note: Previous advice about = and == is incorrect as bash will use either for an equivalence test, however the second is often preferred as being clearer. Assignment in bash requires no space either side of single = for assignment.

veerain 03-08-2015 11:07 AM

Quote:

Originally Posted by grail (Post 5328678)
Note: Previous advice about = and == is incorrect as bash will use either for an equivalence test, however the second is often preferred as being clearer. Assignment in bash requires no space either side of single = for assignment.

I read man page of bash and it says we can use either '=' or '==' for string comparison. My deficit in knowledge. Excuse.

jlinkels 03-08-2015 11:19 AM

There are a number of reasons.

First off, your version of nc seems to be different from mine. Mine doesn't have the "-d" option. Then, my server answers with:
Code:

homeserv.megaline.com [192.168.110.10] 22 (ssh) open
Other than that, it seems that the respons of nc is not written to stdout but to stderr:
Code:

jlinkels@donald-pc:/mnt/homeserv/home/applic/docs/misc/recepten$ var1=$(nc -vzw5 homeserv.megaline.com 22)
homeserv.megaline.com [192.168.110.10] 22 (ssh) open
jlinkels@donald-pc:/mnt/homeserv/home/applic/docs/misc/recepten$ echo $var1
<empty line>

Then if the output of nc was assigned to $var1, it would contain a CRLF at the end, whereas $var2 doesn't have that. You should check on a part of the return string, not on the entire string.

It would be much better to check the result of the nc command by examining the $? variable. It returns 0 on success and 1 on failure of nc.

jlinkels

aristosv 03-08-2015 01:18 PM

I used the $? variable and it worked. thanks

veerain 03-08-2015 09:40 PM

Quote:

Originally Posted by jlinkels (Post 5328739)
Then if the output of nc was assigned to $var1, it would contain a CRLF at the end, whereas $var2 doesn't have that. You should check on a part of the return string, not on the entire string.

jlinkels

In bash if the output of command substitution has trailing newline then it is not saved in a variable.


All times are GMT -5. The time now is 11:40 PM.