Jake Roberts
Jake Roberts

Reputation: 11

Bash test for command exit status

I've created a little rsync script to sync my podcasts. I've configured the script to email me when it's done. I am attempting to test rsync's exit status to decide which message gets sent. Here is the block:

my_command= rsync --log-file=/home/jake/logs/rsync.log -avzu $local_directory  $remote_directory  
if [ $? -eq 0 ]; then  
    $mail_expression_success  
else  
    $mail_expression_fail  
fi  

No matter how the command finishes I get the message contained in the first variable. $mail_expression_success.

Upvotes: 1

Views: 1557

Answers (3)

vis.15
vis.15

Reputation: 741

What you want to do is this:

my_command=$(rsync --log-file=/home/jake/logs/rsync.log -avzu $local_directory  $remote_directory)
if [ $? -eq 0 ]; then  
    $mail_expression_success  
else  
    $mail_expression_fail  
fi  

Upvotes: 1

hendry
hendry

Reputation: 10843

It's better just to do something like this:

if rsync ....
then
       echo Yay
else
       echo Oh noes
fi

Upvotes: 2

unxnut
unxnut

Reputation: 8839

You cannot have a space after =. If you put the space, your value is not properly assigned. You will be better off not assigning the output to f and just checking for the $?.

Upvotes: 0

Related Questions