How to use the return code of the first program in a pipe command line
Asked Answered
S

2

7

I'm writing a simple program that parses the output from a compiler and reformats any error messages so that the IDE we use (visual studio) can parse them. We use nmake to build, and it will call the compiler using a command line like this:

cc166.exe SOME_FLAGS_HERE MyCFile.c 2>&1 | TaskingVXToVisualReformat.exe

Now the problem is that the return code of the compiler, cc166, is not fed back to nmake. Only the return code of my reformatter is used which means that if I return zero from the reformat program, nmake will continue to the build instead of aborting. How can I feed back the return code from the compiler (cc166.exe) to nmake?

Is there any way my reformat program can read the return code of the compiler and use that when deciding its own return code? The reformatter is written in C#.

Sectarian answered 9/5, 2011 at 7:59 Comment(0)
M
2

I would put the compilation instructions in a bash script and make use of its pipefail feature:

The exit status of a pipeline is the exit status of the last command in the pipeline, unless the pipefail option is enabled. If pipefail is enabled, the pipeline’s return status is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands exit successfully.

Let's try it with a simple test:

$ cat bash_pipe.sh
#!/bin/bash
set -o pipefail
ls $1 2>&1 | perl -ne '{print;}'

If we run it with an existing file, the exit code is going to be 0 (passed through the pipe):

$ ./bash_pipe.sh bash_pipe.sh 
bash_pipe.sh
$ echo $?
0

On the other side the command fails with an inexistent file:

./bash_pipe.sh inexistent
ls: cannot access inexistent: No such file or directory
echo $?
2

So in your case you'd need to put the compilation instructions in a script like

$ cat compilation_script.sh
#!/bin/bash
set -o pipefail
cc166.exe SOME_FLAGS_HERE $1 2>&1 | TaskingVXToVisualReformat.exe

and call it directly (if you can) or indirectly through

bash -c "compilation_script.sh MyCFile.c"

Note: the pipefail option was introduced in bash version 3.

Mystify answered 17/11, 2014 at 15:30 Comment(0)
J
0

You can separate your single command into two and keep compilation results in a temporary file:

cc166.exe SOME_FLAGS_HERE MyCFile.c > CCRESULT.TXT 2>&1
if not errorlevel 1 TaskingVXToVisualReformat.exe < CCRESULT.TXT
Jell answered 9/5, 2011 at 11:23 Comment(4)
This does not answer the question. If cc166 succeeds, then it looks like the final return code is also a success. But if it fails, then the reformater's return code is still used: so cc166 fails and reformater succeeds yields in a total return of "success" (when it should be "fail". Furthermore, this assumes that the only failure of cc166 has a return of 1.Handmaid
In windows shell return code 0 is success, any other value is failure. Condition "if not errorlevel 1" actually succeeds if the first command returned 0. FYI, in Windows "if errorlevel 1" means "if error code is greater or equal 1"Jell
I think I understand what You've said so far, but what happens if cc166 returns 1 (or 2, 3, etc) AND TaskingVXToVisualReformat.exe returns 0? will not the whole script return a success, despite the fact that cc166 actually failed?Handmaid
And i do stand corrected about the errorlevel command. Thanks.Handmaid

© 2022 - 2024 — McMap. All rights reserved.