Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > Help with ruby / rsync / awk

Help with ruby / rsync / awk
Thread Tools
Dedicated MacNNer
Join Date: Apr 2005
Status: Offline
Reply With Quote
Aug 25, 2006, 09:00 PM
 
I'm trying to create a backup script in ruby that will trigger a Growl notification to let me know if the sync went through okay. This is what I have so far:

Code:
#!/usr/bin/ruby mail_sync = `rsync -av --delete '/Users/mzllr/Library/Mail/Mailboxes/1' '/Users/mzllr/Documents/Backups/Mail'|awk -F':' '/rsync error/{print $2}'` `/usr/local/bin/growlnotify --title 'Backup' --message '#{mail_sync}' -a 'Backup.app'`
In the terminal, this rsync command outputs an error since there is no "1" source folder, however awk is not passing this info on to the "mail_sync" variable.

Any ideas on where I'm screwing up?

TIA
     
ink
Mac Elite
Join Date: May 2001
Location: Utah
Status: Offline
Reply With Quote
Aug 25, 2006, 09:12 PM
 
The rsync command needs to pipe STDERR to STDOUT. UNIX always maps the file descriptor 0 to standard input, 1 to standard output and 2 to standard error, so:

[FONT="Courier New"]/usr/bin/some/command 2>&1 | /usr/bin/some/other/program[/FONT]

Is what you want it to do (in Bourne-like shells). The portion "2>&1" means "Redirect STDERR to STDOUT".

You might also want to reconsider your plan of attack. You can also check $? (the "exit code") after a command runs (again, with bourne-like shells) to see if they successfully terminated:

[FONT="Courier New"]Inconnito:~ ink$ /usr/bin/true
Inconnito:~ ink$ echo $?
0
Inconnito:~ ink$ /usr/bin/false
Inconnito:~ ink$ echo $?
1
Inconnito:~ ink$ if [ $? == 0 ]; then echo "Hi"; fi
Inconnito:~ ink$ /usr/bin/true
Inconnito:~ ink$ if [ $? == 0 ]; then echo "Hi"; fi
Hi
Inconnito:~ ink$
[/FONT]
(Last edited by ink; Aug 25, 2006 at 09:18 PM. )
     
El Gato  (op)
Dedicated MacNNer
Join Date: Apr 2005
Status: Offline
Reply With Quote
Aug 26, 2006, 12:06 AM
 
Well, damn. I never would've figured that out.

Thanks ink, that worked perfectly.
     
El Gato  (op)
Dedicated MacNNer
Join Date: Apr 2005
Status: Offline
Reply With Quote
Aug 26, 2006, 12:44 AM
 
For anyone interested, here's my final script. Maybe not the best way to do this, but it works. Thanks again for the help, ink.

Code:
#!/usr/bin/ruby sync = [["Mail", nil], ["iCal", nil], ["Address Book", nil]] sync[0][1] = `rsync -a --delete '/Users/mzllr/Library/Mail/Mailboxes/' '/Users/mzllr/Documents/Backups/Mail' 2>&1 | awk -F':' '/rsync error/{print $2}'` sync[1][1] = `rsync -a --delete '/Users/mzllr/Library/Application Support/iCal' '/Users/mzllr/Documents/Backups/iCal' 2>&1 | awk -F':' '/rsync error/{print $2}'` sync[2][1] = `rsync -a --delete '/Users/mzllr/Library/Application Support/AddressBook' '/Users/mzllr/Documents/Backups/Address Book' 2>&1 | awk -F':' '/rsync error/{print $2}'` i = 0 while i < sync.length if (sync[i][1] == '') `/usr/local/bin/growlnotify --title 'Friday Backup' --message '#{sync[i]} has been backed up.' -a 'Backup.app'` else `/usr/local/bin/growlnotify --title 'Friday Backup' --message '#{sync[i]} failed to sync.' -a 'Backup.app'` end i += 1 end
     
   
Thread Tools
Forum Links
Forum 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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 01:15 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2