LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-07-2015, 11:03 PM   #1
aristosv
Member
 
Registered: Dec 2014
Posts: 263

Rep: Reputation: 3
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
 
Old 03-07-2015, 11:08 PM   #2
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Quote:
if [ "$var1" = "$var2" ]; then
You try this:

Code:
if [ "$var1" == "$var2" ]; then
'=' is assignment operator. '==' is string equality operator.
 
Old 03-08-2015, 12:07 AM   #3
aristosv
Member
 
Registered: Dec 2014
Posts: 263

Original Poster
Rep: Reputation: 3
nope, it still reports FALSE :-/
 
Old 03-08-2015, 07:25 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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.
 
Old 03-08-2015, 11:07 AM   #5
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Quote:
Originally Posted by grail View Post
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.
 
Old 03-08-2015, 11:19 AM   #6
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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
 
Old 03-08-2015, 01:18 PM   #7
aristosv
Member
 
Registered: Dec 2014
Posts: 263

Original Poster
Rep: Reputation: 3
I used the $? variable and it worked. thanks
 
Old 03-08-2015, 09:40 PM   #8
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Quote:
Originally Posted by jlinkels View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Running mysql from ssh & query statement has a Text Header in the SELECT statement? djlerman Linux - Server 6 11-19-2013 06:33 PM
[SOLVED] Shell script for adding a statement in a file after a particular statement Aquarius_Girl Programming 4 06-28-2010 03:07 AM
My SQL statement to delete an entry in MYSQL database fails in PHP ratchie Linux - Software 1 12-29-2008 02:48 AM
Problem with if statement in a find -exec statement romsieze Programming 2 10-02-2008 12:38 AM
Joystick fails in MAME, shouldn't be too hard... cfalcon Linux - Games 1 06-06-2005 11:44 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration