LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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-18-2012, 12:31 AM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
if statement test string for value.


sample of my /etc/host file:
Code:
carcapser  192.168.1.6  #prod
ashcapser  192.168.1.7  #prod
carghost   192.168.1.8  #prod
ashghost   192.168.1.9  #prod
carboo     192.168.1.10 #prod
ashboo     192.168.1.11 #prod
carcat     192.168.1.12 #prod
ashcat     192.168.1.13 
carshreik  192.168.1.14 #prod
ashshriek  192.168.1.15
So the script will ssh to any line with a #prod on on it - and (here is where the trouble is)
anything named capser or ghost i want to run one script, and anything else I want to run another.
having trouble though.




Code:
#!/bin/bash

 for i in $(grep "#prod" /etc/hosts | awk '{print $2}')
 do
 ssh -q casper1@$i <<here
 if [ hostname != "casper|ghost' ] ;
 then
  hostname
  echo "/data/noc/crons/ping_gw_test_mcore.sh"
  /data/noc/crons/ping_gw_test_mcore.sh

 echo " "
 else
 hostname
 echo "/data/noc/crons/ping_gw_test.sh"
 /data/noc/crons/ping_gw_test.sh
 echo " "
 fi
here
 done

is there something wrong with the if statment evaluation?
 
Old 06-18-2012, 01:19 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
Code:
grep '#prod' /etc/host | while read ip name comment; do 
...
done
 
1 members found this post helpful.
Old 06-18-2012, 01:32 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
I am a little confused ... isn't the first column in the file the hostname? Why not test prior to ssh?

As for the test, my first would be to say using [[]] over [] can often help eliminate quirky errors, but as for the test itself I see a couple of issues:

1. Calling the command straight as you have the test is now comparing if the string 'hostname' is not equal to the other side (tip: think of how you normally get command output)

2. Is it likely that any of the hosts you are going to are called 'casper|ghost', ie the entire string including the pipe? I would guess not. maybe you are thinking of doing a regex match
which would then need to use =~
 
1 members found this post helpful.
Old 06-18-2012, 04:17 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
Note: Having a file named '/etc/host' is a very bad idea. It could very well be /usr/local/etc/someproject.hostlist or ~casper/someproject.hostlist
 
1 members found this post helpful.
Old 06-18-2012, 05:58 AM   #5
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora and others
Posts: 757

Rep: Reputation: 145Reputation: 145
For a partial match of names, the case statement is best. It matches just like filenames are wildcarded on the command line (not regular expressions).

I didn't try to run it, but this should work:

Code:
#!/bin/bash

grep '#prod' /etc/host | while read IP NAME comment; do 
    case $NAME in
        *casper|*ghost)
            SCRIPT="/data/noc/crons/ping_gw_test.sh"
            ;;
        *)  SCRIPT="/data/noc/crons/ping_gw_test_mcore.sh"
            ;;
    esac
    ssh -q casper1@$IP "hostname; echo $SCRIPT; $SCRIPT"
    echo
done
 
1 members found this post helpful.
Old 06-19-2012, 01:29 AM   #6
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
I have given up on making the <<here document work - for now.

This works - but only sends the script via ssh's to the first host in the list then exits.



Code:
#!/bin/bash
grep '#prod' /etc/hosts | while read IP Name comment;
do
case $NAME in
*casper|*ghost) SCRIPT="/data/noc/crons/ping_gw_test_mcore.sh" ;;
*)SCRIPT="/data/noc/crons/ping_gw_test.sh" ;;
esac
ssh -q casper@$IP "hostname; echo "$SCRIPT" ; $SCRIPT"
echo
done

Last edited by casperdaghost; 06-19-2012 at 08:21 AM.
 
Old 06-19-2012, 08:28 AM   #7
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
When I just print out ip's from the while loop

Code:
casper@casperbox:~$ more /home/casper/case_exp
#!/bin/bash
grep '#prod' /etc/hosts | while read IP Name comment;
do
echo $IP
sleep 1
done

so what I need if for the care statement to be applied to each ip from the /etc/hosts list.

Code:
this is what I get 
168.18.40.49
168.29.80.18
168.29.100.11
168.29.80.16
168.29.80.29
168.29.80.23
168.18.40.18
168.18.60.11
168.18.40.16
168.18.40.38
168.18.40.39
168.18.60.11
168.18.57.11
168.29.97.11
168.18.40.46
168.29.80.47
168.18.72.11
168.18.72.12
168.18.72.13
168.18.72.35
168.18.72.31
168.18.50.20
168.18.50.21
192.168.139.97
168.18.40.49
168.18.50.20

Last edited by casperdaghost; 06-19-2012 at 08:29 AM.
 
Old 06-19-2012, 08:57 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
Somewhere during the saga you switched from /etc/host (name ip comment) to /etc/hosts (ip name comment). Was that intentional?
 
1 members found this post helpful.
Old 06-19-2012, 09:06 AM   #9
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
i am at work now ... the work file i deal with is /etc/hosts .. at home i have a copy of the work file i use for remote connection .. /etc/host
 
Old 06-19-2012, 09:26 AM   #10
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by casperdaghost View Post
This works - but only sends the script via ssh's to the first host in the list then exits.
The ssh commmand is reading stdin so it swallows all the data from grep, you can fix it by redirecting from /dev/null:

Code:
ssh -q casper@$IP "hostname; echo "$SCRIPT" ; $SCRIPT" < /dev/null
# or use -n option for ssh which does the same
ssh -n -q casper@$IP "hostname; echo "$SCRIPT" ; $SCRIPT"
Quote:
man ssh(1)
...
-n
Redirects stdin from /dev/null (actually, prevents reading from
stdin). This must be used when ssh is run in the background. A
common trick is to use this to run X11 programs on a remote
machine. For example, ssh -n shadows.cs.hut.fi emacs & will
start an emacs on shadows.cs.hut.fi, and the X11 connection will
be automatically forwarded over an encrypted channel. The ssh
program will be put in the background. (This does not work if
ssh needs to ask for a password or passphrase; see also the -f
option.)
 
1 members found this post helpful.
Old 06-19-2012, 01:56 PM   #11
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
the -n works great!!!!
and that solves alot of problems that i have been having scripting with ssh.
that is one of the reasons that i originally put this script in the here document.

I really appreciate you help guys - all of you
 
  


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
[SOLVED] how do i test $ in if statement amateurscripter Linux - Newbie 5 11-11-2011 03:56 PM
test statement in bash casperdaghost Linux - Newbie 8 10-13-2011 07:16 AM
ash test: is string A contained in string B chochem Programming 6 09-24-2008 06:59 AM
a string switch statement in c++ pengu Programming 14 02-25-2006 09:58 PM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:38 PM.

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