Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to capture Windows specific exit codes in in git-bash?

I have a scenario where a windows application that I execute in CI exits with -1073740791 eg Stack Overflow. One cmd, this can be caught obviously via %errorlevel% but on bash, at least this exit code maps to 127 in $?.

Obviously, bash on windows should not break scripting so anything above or beyond 0-255 is not fine.

Question is: Is there any special variables or mechanism directly in git-bash itself to catch this actual value ? In this case, the executable is testsuite (think off google benchhmark or google test) and exit code 127 - command not found is not helpful at all.

like image 614
rasjani Avatar asked Nov 14 '25 18:11

rasjani


1 Answers

I had the same issue and i do not think that there is any way to do that within bash. I decided to wrap my executable in a powershell call, append the exit code to stdout and extract it afterwards like this:

OUTPUT=$(powershell ".\"$EXECUTABLE\" $PARAMETERS 2>&1 | % ToString; echo \$LASTEXITCODE")
# Save exit code to separate variable and remove it from $OUTPUT.
EXITCODE=$(echo "$OUTPUT" | tail -n1 | tr -d '\r\n')
OUTPUT=$(sed '$d' <<< "$OUTPUT")

Some notes:

  • This solution does combine all stdout and stderr output into the variable $OUTPUT.
  • Redirecting stderr in powershell wraps the output in some error class. Calling ToString() on these returns them as normal text. This is what the | % ToString is for. Cmp. this SO Answer.
  • Invoking Powershell can be surprisingly slow due to Windows Defender. This can possibly be fixed by adding powershell to the list of Windows Defender exclusions.
like image 197
Jens Avatar answered Nov 17 '25 09:11

Jens