Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a command in my Makefile that sync's up my bibliographies from an external source. If rsync fails because the target file does not exist rsync exits with error 23. I can hide the error by piping the output to /dev/null but this still causes my Makefile to exit. How can I get the following Makefile code to output "Hi" when $(bib_dir) does not exist?

sync_bibliography:
    rsync $(bib_dir) bib 2>/dev/null
    echo "HI"
share|improve this question
    
You can use the OR operator ||: false || echo "Hi" outputs Hi –  Marco Aug 1 '12 at 14:21
    
@Marco That's perfect! I didn't know about the || operator (but I did know about &&, so I guess it was the next step...). Put it in an answer an I'll accept it. –  Hooked Aug 1 '12 at 14:25
add comment

2 Answers

up vote 3 down vote accepted

You can use the OR operator ||. If the previous command fails and return false, the command on the right side is executed. Example:

false || echo "The previous command failed"

or in your case

rsync $(bib_dir) bib 2>/dev/null || echo "HI"
share|improve this answer
    
With your suggestion I went one better and gave myself some feedback: rsync $(bib_bib) bib 2>/dev/null && echo "Bibliography sync complete" || echo "Bibliography sync failed" && true. Thanks for the help! –  Hooked Aug 1 '12 at 14:29
add comment

Put the rsync command inside of a shell script and then call the shell script inside of the Makefile. At the end of the shell script, use something that will always return true such as sleep 0.

share|improve this answer
    
Excuse my shell-ignorance, but doesn't that put me in the same situation? The shell script still fails when rsync fails, does the error not get passed to the script itself? Can you put a MWE that shows how to exit a shell script with no error even if a line fails? –  Hooked Aug 1 '12 at 14:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.