LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-08-2007, 10:21 AM   #1
ricmon
LQ Newbie
 
Registered: Jun 2007
Posts: 9

Rep: Reputation: 0
string comparisons


Can anyone tell me why this string comparison fails.

thanks
ricmon


#!/bin/bash -x
set -v
ssh rmccown@xxx.xxx.xxx.50 /usr/bin/bash <<EOD

listentest=\`lsof -i:7001 | grep LISTEN\`
listening="java 6045 firstgov 31u IPv4 0x30011c80de0 0t0 TCP s262789dc3su12.ushrnd2.savvis.net:7001 (LISTEN)"
echo \$listentest >> listentestworks.txt
echo \$listening >> listingworks.txt
if [ "listentestworks.txt" = \"listingworks.txt\" ]; then
hostname >> lsof_checks.txt
echo "Listner Found" >> lsof_checks.txt
echo \$listentest >> lsof_checks.txt
echo \$listening >> lsof_checks.txt
else
hostname >> no_lsof_checks.txt
echo "no listener" >> no_lsof_checks.txt
echo \$listentest >> no_lsof_checks.txt
echo \$listening >> no_lsof_checks.txt
fi
EOD
exit
 
Old 06-08-2007, 11:40 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

You are comparing the names of 2 files, not the content of those 2 files.

You should do something like this instead, which compares the 2 variables:

if [ "$listentest" == "$listening" ]; then

You should also remove the
echo \$listentest >> listentestworks.txt
echo \$listening >> listingworks.txt
part, these files aren't needed anymore.

Hope this helps.
 
Old 06-08-2007, 11:57 AM   #3
ricmon
LQ Newbie
 
Registered: Jun 2007
Posts: 9

Original Poster
Rep: Reputation: 0
still not working

unfortunately the string comparison still does not working. It can seem to determind if the condition is false.

Output from scripit is put into a if true file lsof_checks.txt and a if false file called no_lsof_checks.txt. When i put characters in the string that schould produce a false condition the out put data is still going to the true file.

output from file clearly shows that the strings don't match

$ cat lsof_checks.txt
s262789dc3su12
Listner Found
java 6045 firstgov 31u IPv4 0x30011c80de0 0t0 TCP s262789dc3su12.ushrnd2.savvis.net:7001 (LISTEN)
342gwejava 6045 firstgov 31u IPv4 0x30011c80de0 0t0 TCP s262789dc3su12.ushrnd2.savvis.net:7001 (LISTEN)
 
Old 06-08-2007, 01:02 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

The way you use ssh is not correct. Try it this way:

Code:
#!/bin/bash
ssh -T stasis <<EOD
listentest=`lsof -i:7001 | grep LISTEN`
listening="java 6045 firstgov 31u IPv4 0x30011c80de0 0t0 TCP s262789dc3su12.ushrnd2.savvis.net:7001 (LISTEN)"
if [[ $listentest == $listing ]]
then
  hostname >> lsof_checks.txt
  echo "Listner Found" >> lsof_checks.txt
  echo $listentest >> lsof_checks.txt
  echo $listening >> lsof_checks.txt
else
  hostname >> no_lsof_checks.txt
  echo "no listener" >> no_lsof_checks.txt
  echo $listentest >> no_lsof_checks.txt
  echo $listening >> no_lsof_checks.txt
fi
EOD
exit 0
Hope this helps.

Last edited by druuna; 06-08-2007 at 01:20 PM. Reason: Made the if statement syntactically correct
 
Old 06-08-2007, 01:05 PM   #5
dawkcid
Member
 
Registered: May 2007
Location: UK
Distribution: LFS,Slackware,Slamd64,NetBSD
Posts: 102

Rep: Reputation: 15
You are using mixed semantics. The shell has two different syntaxes for comparisons

[ is a synonym for the test function. To compare strings with this, you need the = operator

if [ string1 = string2 ];

(one = sign)

The (newer) POSIX syntax requires double brackets [[, thus

if [[ string1 == string2 ]];

(two = signs)

The [[ syntax is generally easier to read and use, so you should probably use that unless you need compatibility with old pre-POSIX shells.

Last edited by dawkcid; 06-09-2007 at 01:41 PM. Reason: syntax correction
 
Old 06-11-2007, 08:54 AM   #6
ricmon
LQ Newbie
 
Registered: Jun 2007
Posts: 9

Original Poster
Rep: Reputation: 0
Still not working

Thanks for all the help. But I'm still not getting excpected results. So the question are these strings identical?

java 6045 firstgov 31u IPv4 0x30011c80de0 0t0 TCP s262789dc3su12:7001 (LISTEN)
java 6045 firstgov 31u IPv4 0x30011c80de0 0t0 TCP s262789dc3su12:7001 (LISTEN)

The output of this script is going to the no match found file (no_lsof_checks.txt).
 
Old 06-11-2007, 09:37 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

Did you try it both ways (yours, using ssh .. /usr/bin/bash and 'mine' using ssh -T and the alternate layout)?

Another thing: The strings you show (both posts 1 and 6) are not the same as lsof's output (more spaces between certain fields). This could be because you didn't use tags around it when posting.

Output looks something like this:
Code:
cupsd     0000    user    0u  IPv4   0000       TCP localhost.localdomain:ipp (LISTEN)
The strings you show in post 6 look the same, but could be different. From the example it is not clear if there are spaces and/or tabs present. And x(space)y is not equal to x(tab)y.

Hope this helps
 
Old 06-11-2007, 11:05 AM   #8
ricmon
LQ Newbie
 
Registered: Jun 2007
Posts: 9

Original Poster
Rep: Reputation: 0
Yes I did try the new syntax still the conditional test is still failing. Thought I would take a different approch using cksum's for the test.

#!/bin/bash -x
set -v
ssh -T me@xxx.xxx.xxx.xxx <<EOD
listentest=\`lsof -i:7001 | grep LISTEN\`
listening="java 6045 firstgov 31u IPv4 0x30011c80de0 0t0 TCP s262789dc3su12.ushrnd2.savvis.net:7001 (LISTEN)"
echo \$listentest > listentest_checks.txt
echo \$listening > listening_checks.txt
cksum listentest_checks.txt > listentest
cksum listening_checks.txt > listening
if [[ listentest == listening ]]
then
hostname >> lsof_checks.txt
echo "Listner Found" >> lsof_checks.txt
echo \$listentest >> lsof_checks.txt
echo \$listening >> lsof_checks.txt
else
hostname >> no_lsof_checks.txt
echo "no listener" >> no_lsof_checks.txt
echo \$listentest >> no_lsof_checks.txt
echo \$listening >> no_lsof_checks.txt
fi
EOD
exit 0

output from cksum test

$ cat listening
1807018323 98 listening_checks.txt
$ cat listentest
1807018323 98 listentest_checks.txt

thus a the test statement is true but still getting false results

Last edited by ricmon; 06-11-2007 at 11:06 AM.
 
Old 06-11-2007, 11:28 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

The above code you posted is _not_ what I posted/proposed! You are mixing escaped characters with none-escaped characters. You should not escape the $ and the ` (backtick) if you use -T. I.e:

listentest=\`lsof -i:7001 | grep LISTEN\` should be listentest=`lsof -i:7001 | grep LISTEN` (listentest="`lsof -i:7001 | grep LISTEN`" is even better).

and

echo \$listentest >> lsof_checks.txt should be echo $listentest >> lsof_checks.txt
 
Old 06-11-2007, 12:19 PM   #10
ricmon
LQ Newbie
 
Registered: Jun 2007
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by druuna
Hi,

The above code you posted is _not_ what I posted/proposed! You are mixing escaped characters with none-escaped characters. You should not escape the $ and the ` (backtick) if you use -T. I.e:

listentest=\`lsof -i:7001 | grep LISTEN\` should be listentest=`lsof -i:7001 | grep LISTEN` (listentest="`lsof -i:7001 | grep LISTEN`" is even better).

and

echo \$listentest >> lsof_checks.txt should be echo $listentest >> lsof_checks.txt

When I remove the escape character the output files
echo $listentest > listentest_checks.txt
echo $listening > listening_checks.txt
are empty. When I escape $listening data is put in the text files.

#!/bin/bash -x
set -v
ssh -T rmccown@10.35.25.50 <<EOD
listentest="`lsof -i:7001 | grep LISTEN`"
listening="java 6045 firstgov 31u IPv4 0x30011c80de0 0t0 TCP s262789dc3su12.ushrnd2.savvis.net:7001 (LISTEN)"
echo $listentest > listentest_checks.txt
echo $listening > listening_checks.txt
cksum listentest_checks.txt > listentest
cksum listening_checks.txt > listening
if [[ listentest == listening ]]
then
hostname >> lsof_checks.txt
echo "Listner Found" >> lsof_checks.txt
echo $listentest >> lsof_checks.txt
echo $listening >> lsof_checks.txt
else
hostname >> no_lsof_checks.txt
echo "no listener" >> no_lsof_checks.txt
echo $listentest >> no_lsof_checks.txt
echo $listening >> no_lsof_checks.txt
fi
EOD
exit 0
 
Old 06-11-2007, 01:27 PM   #11
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

I see that you are still mixing your old code and the one I gave. You do not need output files just the variables, like I stated in post #2 and shown in post #4. You also did not use dollarsigns in the if [[ ]] statement.

You are correct in stating that the $ should be escaped (\$).

Ok, below is a version that works.
Code:
#!/bin/bash
#set -xv

ssh -T rmccown@10.35.25.50 <<EOD
listentest="`lsof -i:7001 | grep LISTEN`"
listening="java 6045 firstgov 31u IPv4 0x30011c80de0 0t0 TCP s262789dc3su12.ushrnd2.savvis.net:7001 (LISTEN)"

# Do both strings match
if [[ \$listentest == \$listening ]]
then
  # Yes they do, put all output in lsofchecks.txt
  hostname >> lsof_checks.txt
  echo "Listner Found" >> lsof_checks.txt
  echo \$listentest >> lsof_checks.txt
  echo \$listening >> lsof_checks.txt
else
  # No they don't, put all output in no_lsof_checks.txt
  hostname >> no_lsof_checks.txt
  echo "no listener" >> no_lsof_checks.txt
  # next line is not needed, $listentest will always be empty
  echo \$listentest >> no_lsof_checks.txt
  echo \$listening >> no_lsof_checks.txt
fi
EOD
exit 0
You do need to edit the listening="java 6045........ line, there should be more spaces.

Hope this helps.

Last edited by druuna; 06-11-2007 at 02:12 PM. Reason: Added some comments
 
Old 06-11-2007, 01:40 PM   #12
ricmon
LQ Newbie
 
Registered: Jun 2007
Posts: 9

Original Poster
Rep: Reputation: 0
Question

Quote:
Originally Posted by druuna
Hi,

I see that you are still mixing your old code and the one I gave. You do not need output files just the variables, like I stated in post #2 and shown in post #4. You also did not use dollarsigns in the if [[ ]] statement.

You are correct in stating that the $ should be escaped (\$).

Ok, below is a version that works.
Code:
#!/bin/bash
#set -xv

ssh -T rmccown@10.35.25.50 <<EOD
listentest="`lsof -i:7001 | grep LISTEN`"
listening="java 6045 firstgov 31u IPv4 0x30011c80de0 0t0 TCP s262789dc3su12.ushrnd2.savvis.net:7001 (LISTEN)"
if [[ \$listentest == \$listening ]]
then
  hostname >> lsof_checks.txt
  echo "Listner Found" >> lsof_checks.txt
  echo \$listentest >> lsof_checks.txt
  echo \$listening >> lsof_checks.txt
else
  hostname >> no_lsof_checks.txt
  echo "no listener" >> no_lsof_checks.txt
  echo \$listentest >> no_lsof_checks.txt
  echo \$listening >> no_lsof_checks.txt
fi
EOD
exit 0
You do need to edit the listening="java 6045........ line, there should be more spaces.

Hope this helps.

This is the out put from the fail test results:
cat no_lsof_checks.txt
s262789dc3su12
no listener

java 6045 firstgov 31u IPv4 0x30011c80de0 0t0 TCP s262789dc3su12.ushrnd2.savvis.net:7001 (LISTEN)

As you can see echo \$listentest >> no_lsof_checks.txt is not getting written to. Here in is the problem. Thus the script still does not seem to be working properly. Sorry to be a pain but I can not deternmind why this simple script won't work.
 
Old 06-11-2007, 01:55 PM   #13
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

The output _is_ written to your no_lsof_checks.txt file. It's the empty line between 'no listener' and 'java 6045 firstgov......'

Lsof's output are connection listening on port 7001. If there aren't any, no output is generated. Then you grep LISTEN from the output from lsof. If there are connections on port 7001, but they do not have the LISTEN state, nothing is printed and if the output from lsof was empty already, nothing is printed.

Works as designed

Hope this clears things up.
 
Old 06-11-2007, 02:03 PM   #14
ricmon
LQ Newbie
 
Registered: Jun 2007
Posts: 9

Original Poster
Rep: Reputation: 0
this is the command run from the command line.

listentest="`lsof -i:7001 | grep LISTEN`"

$ lsof -i:7001 | grep LISTEN
java 6045 firstgov 31u IPv4 0x30011c80de0 0t0 TCP s262789dc3su12.ushrnd2.savvis.net:7001 (LISTEN)

This out put should go in no_lsof_checks.txt:
\$listentest >> no_lsof_checks.txt
 
Old 06-11-2007, 02:13 PM   #15
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

No, it will go into lsof_checks.txt if it is actually found, if nothing is found an empty line is printed in no_lsof_checks.txt.

The program will only end up in the 'no listener' section if nothing is found (lsof -i:7001 | grep LISTEN has no output), if the output matches the string you will end up in the listener found section.

Take another look at post #11, I put some comments in them.
 
  


Reply

Tags
string


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
CPU comparisons lothario Linux - Hardware 2 12-11-2006 08:25 PM
Discuss: Performance Comparisons confused_bof Linux - General 4 12-22-2005 03:40 PM
Perl string comparisons Xris718 Programming 5 04-03-2005 10:43 AM
Collecting TCO and other comparisons ggreaves General 1 02-02-2005 10:35 AM
Firewall Comparisons Please floppywhopper Linux - Security 11 10-02-2004 07:42 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration