It is currently Sat Oct 21, 2017 10:04 am

Reading from file, and turn it into variables

All times are UTC - 6 hours

Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Tue May 05, 2009 1:25 am   

Joined: Mon May 04, 2009 10:18 pm
Posts: 1
Hi All,

I'm new to scripting and bash altogether, so don't bash me on such a nub question! :shock:
I have a file say serverlist.lst, inside this file:

If colon is the delimiter, how do I turn each one into $1, $2, $3 and so on

What I'm trying to achieve here is to run an rsync script, that would read $1 as the servername, then $2 to $blah as the folders to rsync.

So essentially it'll look like rsync $RSYNC_OPTS [email protected]$1:$2 $backupdir, then same line with $3 and so on
Once this line completes it'll run the same thing but for the new line.

Any help would be appreciated. Thanks!

 PostPosted: Tue May 05, 2009 2:19 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Well you don't need them to be named $1 and so on.

If you're parsing the lines from a textfile in your bashscript you can just set them to a variable name.

Like this:
# input file, you can replace this with $1 so you can run the script with ./ server_list.txt
# Your rsync options and executable things
RSYNC_OPTS="rsync arguments and stuff"

# Open the file and process all the lines in it.
for i in $(cat $input_file); do
   # Cut out everything up till the delimiter kolon.
   hostname=$(echo $i | cut -d':' -f1)
   # Cut out everthing after the first kolon until the next one
   target=$(echo $i | cut -d':' -f2)
   # This one is a tricky one. It ignores the first 2 kolons and then saves everything that comes after.
   # [^:]\+ tells sed to position at the next kolon
   paths=$(echo $i | sed "s/^[^:]\+:[^:]\+:\(.*\)$/\1/")
   # Run the rsync
   rsync $RSYNC_OPTS [email protected]$hostname:$target $paths

This haven't been tested at all, but it could look something like this.

Best regards
Fredrik Eriksson

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours

Who is online

Users browsing this forum: Bing [Bot] and 20 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  

BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group