Exit after git completed

I am running a simple script

#!/bin/bash
git fetch --all 2> stderr.txt
echo "Errorcode $?"
cat -n stderr.txt
echo Sleeping
sleep 1
cat -n stderr.txt

The strange point is the conclusion

Fetching origin
Errorcode 1
     1  fatal: Couldn't find remote ref HEAD
     2  error: Could not fetch origin
Sleeping
     1  fatal: Couldn't find remote ref HEAD
     2  error: Could not fetch origin
     3  fatal: The remote end hung up unexpectedly

And how can I clear the output and still have access to the error code?

I tried it with stdbuf -o0 -e0, but no luck.

Cleaning works with tee, but then the error code is lost.
git fetch --all 2>&1 | tee stderr.txt > /dev/null

Note:
- An error is expected in this scenario.
- The problem also occurs without stderr redirection.
- I used to git version 2.11.0.

+4
source share
1 answer

, Git , . Git, , stderr, Git .

, Git ( , , ). , , :

if ! git fetch --all 2> stderr.txt; then
    failure=$?
    sleep 0.5 # or however long you think is appropriate
    ... do something with stderr.txt
    ... use the saved failure code in $? ...
else
    # the git fetch worked; $? is 0; no need to sleep
    ...
fi

, , bash $PIPESTATUS. , , tee :

any-command-here 2>&1 | tee ...

, tee, , , , tee.

+2
source

Source: https://habr.com/ru/post/1693605/


All Articles