0

I am running some unit tests which involve piping into my executable, like this.

cat text.txt | my_exe --options

Running these commands, how do I get the return value of my_exe? Even when my_exe returns failure, the return value is discarded. I assume the return value of cat is prioritized instead.

Asked another way, how do I execute the above pipe command to give the same return value as :

cat text.txt && my_exe --options

ty!

edit : Using the traditional cmd.

3
  • 2
    Which command-line shell are you using? Commented Feb 24 at 17:06
  • 2
    What do you mean by "the return value is discarded" and how are you checking for it?
    – harrymc
    Commented Feb 24 at 17:18
  • @u1686_grawity Edited the question with the answer, cmd. @harrymc Unit tests are ran on a CI server, which notifies of failure of the command if return value != 0.
    – scx
    Commented Feb 25 at 19:29

1 Answer 1

1

cmd DOES set the exit status for a pipeline to the rightmost component and the (or any) other component is NOT 'prioritized' or even considered. Observe:

c:\temp>echo exit 42 | cmd >NUL

c:\temp>echo %errorlevel%
42


c:\temp>cmd/c echo exit 42 | cmd >NUL

c:\temp>echo %errorlevel%
42


c:\temp>cmd/c "echo exit 42 && exit 17" | cmd >NUL

c:\temp>echo %errorlevel%
42


c:\temp>cmd/c exit 17 | cmd/c exit 0

c:\temp>echo %errorlevel%
0


c:\temp>cmd/c exit 11 | cmd/c exit 22 | cmd/c exit 33

c:\temp>echo %errorlevel%
33


c:\temp>cmd/c exit 11 | cmd/c exit 22 | cmd/c echo GOOD
GOOD

c:\temp>echo %errorlevel%
0

If you're getting a different result you must be doing something different than you show in your Q, but since we have no clue what that is, it's impossible to make any useful suggestions.

1
  • You are correct. The server was running powershell instead of cmd. Switching to cmd fixed the problem. Thank you for answering though!
    – scx
    Commented Mar 2 at 15:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .