Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use info from file to use in bash script

Status
Not open for further replies.

proggybilly

Programmer
Apr 30, 2008
110
US
I have a script that creates a tbz file as a "backup" of some system directories on a linux server and then rsync's that to another server, the code looks like this:

Code:
#!/bin/sh
/usr/bin/find /backup -type f -ctime +1 ! -exec rm {} \;
/usr/bin/find /var/lib/asterisk/backups -type f -ctime +7 ! -exec rm {} \;

cd /

filename="system-`date +'%Y%m%d%H%M'`.tbz2"
filenm1="/backup/$filename"

START=`date +%s`
START1=`date  +"%Y-%m-%d %H:%M:%S"`

tar -jcf $filenm1 -X /etc/backup.exclude ./usr/lbin ./etc ./var

FILESBACK=`grep -c "/" $filenm1`
FILESIZE=`du -shm $filenm1`

RSYNC_PASSWORD=Asbak12
export RSYNC_PASSWORD

cd /backup
/usr/bin/rsync -r -p --force $filename astback@dis::astback

I want to pull config information from another file, the file info looks like this:
172.16.254.3
255.255.255.0
172.16.254.1
172.16.254.105
197
198
196
M
172.16.254.20
phonesync
12345
phonesync
on

I need to first check if the last line equals on, then I need to pull data from the file, the info I need is from below the M to the end so that my code will now look like this:

Code:
#!/bin/sh
/usr/bin/find /backup -type f -ctime +1 ! -exec rm {} \;
/usr/bin/find /var/lib/asterisk/backups -type f -ctime +7 ! -exec rm {} \;

cd /

filename="system-`date +'%Y%m%d%H%M'`.tbz2"
filenm1="/backup/$filename"

START=`date +%s`
START1=`date  +"%Y-%m-%d %H:%M:%S"`

tar -jcf $filenm1 -X /etc/backup.exclude ./usr/lbin ./etc ./var

FILESBACK=`grep -c "/" $filenm1`
FILESIZE=`du -shm $filenm1`

RSYNC_PASSWORD=[b]12345[/b]
export RSYNC_PASSWORD

cd /backup
/usr/bin/rsync -r -p --force $filename [b]phonesync[/b]@[b]172.16.254.20[/b]::[b]phonesync[/b]

I bolded the changes that would come from the config file.

I need to know how to pull that data into an array and get the total number of elements so i can do something similar to
Code:
if !$ARRAY[$i] == on then exit else continue
then I need the specific items starting at the last element and working backwards to populate into the areas you see in bold.

Please, can anyone help. I am a severe newb at bash scripting.
 
Can you be confident that none of those last 5 lines will be empty, and that each is a single word? If so, this should work:

Code:
# assign rest of command line to positional parameters $1, $2, $3, etc.
set -- $(tail -5 configfile)

if [[ "$5" == "on" ]]
then
        exit
else
        cd /backup
        /usr/bin/rsync -r -p --force $filename $2@$1::$4
fi

If not, we'll need to come up with something a little smarter.

Annihilannic.
 
Yes I can guarantee that the file will be as I described. It is populated from a web form that I created. I will give your example a shot today. Thanks!
 
Worked like a charm! Now to work out the kinks. Trying to rsync from linux to windows so there are many firewall issues to work out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top