1

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.

3
  • 2
    Where are you executing the rsync command? It seems like you are just assigning it to a variable, which always succeeds. Commented Oct 15, 2013 at 2:15
  • 1
    Why do you think the rsync is failing? The code you have posted here looks fine (but a little odd, since the assignment of the null string to my_command is unused). Commented Oct 15, 2013 at 2:30
  • Two issues: if my_command="rsync..." is intended, then it really is a variable assignment which always succeeds, although rsync is never actually called. If the intention is to capture the output of rsync with my_command=$(rsync ...), then rsync is called, but instead of assigning the output to my_command, rsync is simply run in an environment where my_command is null valued (and ignored by rsync). In the latter case, it does seem, as William Pursell points out, that rsync is simply succeeding. Commented Oct 15, 2013 at 12:40

3 Answers 3

2

It's better just to do something like this:

if rsync ....
then
       echo Yay
else
       echo Oh noes
fi
Sign up to request clarification or add additional context in comments.

Comments

1

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  

1 Comment

It is reasonable to assume that the OP intended to do an assignment like this, but the question as posted works just fine and makes the same assignment to $? as does this solution, so something else is happening.
0

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 $?.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.