 |
 |
Help with ruby / rsync / awk
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Apr 2005
Status:
Offline
|
|
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
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: May 2001
Location: Utah
Status:
Offline
|
|
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.
)
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Apr 2005
Status:
Offline
|
|
Well, damn. I never would've figured that out.
Thanks ink, that worked perfectly.
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Apr 2005
Status:
Offline
|
|
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
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |