Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 11-23-2004, 05:27 PM   #1
Senior Member
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Rep: Reputation: 52
Bash script - reading from text file

Hi There,

Got a question for you bash experts out there . I have a file (client_ip): twantrd johntrd ricktrd

As you can see, it contains the ip address and hostname of the machine associated with it. I pretty much need a bash script that will ssh into each ip and grab files to be backed and store it back on the server under the appropriate hostname. So it's like:

Server A ---(ssh)---> ---(rsync)---> Server A:/home/backup/twantrd/

Server A ---(ssh)---> ---(rsync)---> Server A:/home/backup/johntrd/


I have no problem creating a for loop to iterate through the file and grab the IP's and ssh'ing into them. My big problem is how to rsync the data to the appropriate hostname folder on ServerA. My partial script is below. Please help if someone can!! I truly appreciate it!! Thanks!


clientip=`cat client_ip | egrep -v '^#' | awk '{print $1}'`
clienthost=`cat client_ip | egrep -v '^#' | awk '{print $2}'`

#This will rsync the client's data to the server
for ip in $clientip; do
$ssh $ip "(cd $backupdir; rsync -avz * $myserver:$dir)"

P.S Forgot, this is all done on the server side (Server A).


Last edited by twantrd; 11-23-2004 at 05:29 PM.
Old 11-23-2004, 08:54 PM   #2
Senior Member
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Rep: Reputation: 58
I think you could cycle through each line of the hosts file like this:
for line in `cat $hosts | egrep -v '^#'` ; do
client_ip=${line/ */}
client_host=${line/* /}
#Put your ssh command here

Note: your variable "client_ip" refers to two different things, thats not good: first it stores the name of the file, then it stores the ip of each client. So I changed the name of the variable storing the filename to "hosts".

Last edited by J_Szucs; 11-23-2004 at 09:02 PM.
Old 11-23-2004, 10:59 PM   #3
Senior Member
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Original Poster
Rep: Reputation: 52
Thanks for the input but that still didn't work. "client_ip" and "client_host" both have the ip address and the hostname. Hmmm....

Old 11-23-2004, 11:20 PM   #4
Registered: Aug 2004
Location: India
Distribution: Redhat Enterprise Server 2.1
Posts: 96

Rep: Reputation: 15

Are you having an issue with the with scripting or rsync not working between server A and the clients
Old 11-24-2004, 12:38 AM   #5
Senior Member
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Original Poster
Rep: Reputation: 52
I *was* having an issue with just iterating the ip address with the associated hostname in the for loop section. Pretty much I wanted to ssh into each of those ip's and rsync the data back to Server A under the path of the hostname that was associated with just that IP. Hmmmm, that sounded kind of confusing .

Anyhow, I just wanted to say thanks to those who tried to help. I got the script working. I was working on this and all of a sudden a lightbulb came on in my head and I found out how. If u guys are interested...I'll post the code. Thanks again!!!



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
Saving a text file as a variable and reading it every second in java script mrobertson Programming 4 03-26-2007 08:25 PM
BASH script Ė reading and writing variables to a separate file morrolan Programming 10 09-20-2005 07:45 AM
Reading a conf file from a BASH script dinolinux Programming 5 08-03-2005 04:18 AM
Need help reading text file in bash script scilec Programming 3 11-25-2004 06:44 PM
reading file, bash script marri Programming 3 11-15-2004 09:13 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:07 AM.

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