Stop git from writing non-errors to stderr

前端 未结 1 1820
無奈伤痛
無奈伤痛 2021-01-13 16:14

I have a script that I am using to automatically sync various remote git repositories. One thing I am trying to do with my scripts is to capture the output of stderr from ev

相关标签:
1条回答
  • 2021-01-13 17:00

    write all those errors into a text file

    Those are not always error, considering most Git commands outputs information message on stderr, as I mentioned here:

    stderr as it is just informative messages, not to be consumed by machines.

    If it better to test the exit status of the command and email both stdout and stderr if said exit status differs from 0

    Plus, you are doing two redirections: > followed by >: the second one would recreate /tmp/stderr-contents-sync_git_repositories.txt: that second redirection should be >>, not >.

    So:

    git fetch --prune-tags github-fetch master > tmp 2>&1 || cat tmp > '/tmp/stderr-contents-sync_git_repositories.txt'
    git push github master > tmp 2>&1 || cat tmp >> '/tmp/stderr-contents-sync_git_repositories.txt'
    

    Here I override a tmp file on each command (with their stdout/stderr), and if that command fails, I write to, or I append to /tmp/stderr-contents-sync_git_repositories.txt.

    This is easier than your edit, where you redirect both commands to a file, even if one of them might have failed.

    That is why I do cmd1 || cat >> file: the >> part only runs if cmd1 fails.

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