ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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
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.
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
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.
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
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 (\$).
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.