2

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.

rasjani
  • 7,372
  • 4
  • 22
  • 35
  • The Windows inventors, in their infinite wisdom, decided that the exit code of a process can be any 32-bit number. Cygwin, which is modelled after Linux (respectively POSIX), the exit code is a 8-bit number, and even then, status codes of 127 and above are used for specific purposes (failure to start a program etc.) means that you effectively have only 7 bit available. You could write a wrapper batch script, which writes the long exit code to a file, and have your bash script evaluate this file. – user1934428 Sep 22 '21 at 08:04

1 Answers1

1

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.
Jens
  • 23
  • 4