Is it possible to do something like this?
if
rsync $folder/coopshop/prod $folder2/coopshop
rsync $folder/coopweb/prod $folder2/coopweb
rsync $folder/unihobbyshop/shop $folder2/unihobbyshop
rsync $folder/unnihobbyweb/web $folder2/unihobbyweb
then
echo "OK! rsync done!"
else
echo "Error! rsync crashed!"
fi
I know there is a possibility to do with \
character at the end of the line, but this is not quite an elegant solution. Is there a better way to do this?
Consider
set -e
.If this is in a script, then you'll probably want to remove the
if
...then
entirely and just putset -e
at the top of the script. This causes the script to stop immediately if any command fails. When any of thosersync
commands fail, they'll display error messages explaining the failure, so you probably don't need to write your own message at all.If you want to show the commands that are run before running them, use
set -x
. You can combine the two by writingset -ex
.With
&&
, you don't need\
.If you do decide to write something along the lines of what you've shown, then as Ronny Blomme says you can separate the commands with
&&
. Note that you don't need to run the commands in subshells, i.e., you don't need to enclose them in parentheses.If you write an
&&
operator at the end of a line, then you don't need to write\
to specify line continuation. A command can't end in&&
(logical AND) or||
(logical OR) so the shell will continue parsing onto the next line.You don't have to put
&&
at the end of a line. You can write multiple commands on the same line separated by&&
if you like.Note that the final command being tested doesn't end with
&&
because&&
separates the commands. If any command fails, the others will not run.Running all the commands even if some of them fail
You may instead want to run all the
rsync
commands even if some of them fail, then test if any of them failed and operate accordingly. For this, you can trapERR
. This is not an actual signal, but theERR
signal_spec in bash catches commands that fail for any reason, including a crash (and including SIGINT). See pLumo's answer (but omitset -e
to keep going) and this post by William Pursell; for more general information, see the output ofhelp trap
.The way I like to do it (with commands like yours plugged in) is:
About the other changes I've made... and what
if command
really tests.Your arguments to
rsync
use parameter expansion so I've taken the liberty of double-quoting them. Otherwise, word splitting and globbing will be performed. From context it appears you don't want those additional expansions. (Usually you don't.)Note that, contrary to the message your script prints, you are not actually testing if any
rsync
command crashed. You are instead testing if anyrsync
command failed. Crashing is abnormal program termination, where a process is terminated by a signal, typically due to a bug or a condition that cannot be reasonably handled. This is far rarer than normal program termination, which may itself occur with an exit status indicating success (zero) or failure (some other number). Ifrsync
does crash, the code above will catch that too. But most of what it's catching, in practice, is normal program termination with an exit status indicating failure.For a script like yours, I would not use
if
/else
, but enable exit on error (set -e
) and trapping.Something like this:
try this syntax:
I tried this.
It works. Problem is when I add for example ls -la to commands, it failed with this.
I don't know why.