How to do a hg update or clone in one line of bash?

前端 未结 2 1144
日久生厌
日久生厌 2021-01-21 03:12

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).

2条回答
  •  青春惊慌失措
    2021-01-21 03:43

    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 
    

    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.

提交回复
热议问题