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

前端 未结 2 1108
日久生厌
日久生厌 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 <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.

    0 讨论(0)
  • 2021-01-21 03:51
    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.

    0 讨论(0)
提交回复
热议问题