I am looking for an option to do an hg update on a repository or to clone it if it doesn\'t exist. So the command has to fail only if something went wrong (clone or update).
The disjunction operator in bash, ||
, can do this scenario: try first command, if it fails try second command, if that fails return the error code. Specific to this question,
hg update || hg clone <source>
would try the update; if it fails, it would try to clone. If cloning fails, the whole line exits with clone's error code.
I would like not to see any errors in the output when the command succeeds.
Why should there be errors when the command succeeds?
You can kill the output by >/dev/null
; you can kill the errors by 2>/dev/null
; whether on each component separately, or by enclosing the whole line in parentheses and putting redirection after.
cd <directory> && hg update || hg clone <source>
If the cd fails because directory doesn't exist, then the hg clone is run from the correct directory level.