LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 09-25-2013, 06:47 AM   #1
keif
Member
 
Registered: Apr 2013
Posts: 107

Rep: Reputation: Disabled
if statement to see if line is in file


Good Morning,

I want to set up a script that will ssh into a host and check for a line in a file. If the line is there it prints "Yes" and if its not there it prints "No".

Here is the lines I have so far:

Code:
#!/bin/sh
for HostName in `cat /home/techs/hostlist | awk '{print $1}'`

do
        echo $HostName
        ssh root@$HostName "grep EXCENTUS_RETAIL_ID /usr1/vision/vision.cnf"
done
At this point, if the line EXCENTUS_RETAIL_ID is in the file then I want to Print "Yes" and if it is not there, I want to print "No".

Any assistance is greatly appreciated. Thanks.
 
Old 09-25-2013, 07:15 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
I will leave the ssh part to someone with more knowledge, however your for loop is a nightmare.
Read files with while loops:
Code:
while read -r HostName _
do
    <your stuff here>
done</home/techs/hostlist
The underscore simply soaks up the rest of the line and assumes the data is white space separated
 
1 members found this post helpful.
Old 09-25-2013, 08:12 AM   #3
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
One thing to watch out for using a while read with redirected input, is that none of the commands inside the loop should read from it; ssh has the -n option for this:
Code:
while read -r HostName _
do
        echo $HostName
        ssh -n root@$HostName "grep -q EXCENTUS_RETAIL_ID /usr1/vision/vision.cnf && echo yes || echo no"
done</home/techs/hostlist
 
1 members found this post helpful.
Old 09-25-2013, 08:24 AM   #4
keif
Member
 
Registered: Apr 2013
Posts: 107

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
I will leave the ssh part to someone with more knowledge, however your for loop is a nightmare.
Read files with while loops:
Code:
while read -r HostName _
do
    <your stuff here>
done</home/techs/hostlist
The underscore simply soaks up the rest of the line and assumes the data is white space separated
Thank you for the advice on using while instead of using cat for the file. I can see why this makes much more sense.

Keith
 
Old 09-25-2013, 08:25 AM   #5
keif
Member
 
Registered: Apr 2013
Posts: 107

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ntubski View Post
One thing to watch out for using a while read with redirected input, is that none of the commands inside the loop should read from it; ssh has the -n option for this:
Code:
while read -r HostName _
do
        echo $HostName
        ssh -n root@$HostName "grep -q EXCENTUS_RETAIL_ID /usr1/vision/vision.cnf && echo yes || echo no"
done</home/techs/hostlist
Thank you. This worked perfectly.
 
  


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] Running if statement from command line? mrm5102 Linux - General 4 04-02-2012 03:10 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
Scripting question - feed an input file into an if statement line-by-line kmkocot Linux - Newbie 10 01-18-2010 11:49 AM
Issue a command line statement from within a C/C++ program? DiBosco Programming 7 05-23-2008 07:34 PM
write line using php if statement and sessions? ati Programming 18 05-09-2006 07:16 AM

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

All times are GMT -5. The time now is 03:57 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