Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

    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
up vote 4 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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .