2

In bash scripts, we can find the exit status of individual commands, which are piped to each other.
e.g. in below pseudo code:

 $ command1 | command2 | command3

The exit status of command1, command2 & command3 can be obtained in ${PIPESTATUS[0]}, ${PIPESTATUS[1]} & ${PIPESTATUS[2]} respectively.

Also, the exit status of last command (command3 in this case) can be obtained from $?.

In case of windows batch scripts, we can find the exit status of the last command with %ERRORLEVEL%. Thus I would say, nearest equivalent of $? in batch scripting is %ERRORLEVEL%.

What is the equivalent of PIPESTATUS in batch scripting? How to find the exit status of individual commands?

anishsane
  • 20,270
  • 5
  • 40
  • 73
  • google gave results only related to `bash` & I could not find related answer on `SO` also. – anishsane Dec 27 '13 at 06:30
  • See [Windows command interpreter: how to obtain exit code of first piped command](http://stackoverflow.com/q/11170753/1012053) – dbenham Dec 27 '13 at 13:45

2 Answers2

3

No such animal. If you want the individual statuses, you'd need

command1 >tempfile
set status1=%errorlevel%
command2 <tempfile >anothertempfile
set status2=%errorlevel%
command3 <anothertempfile
set status3=%errorlevel%
Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Hmm... actually, the purpose of piping is to show a kind of progress bar, for first command. My current implementation is based on [this answer](http://stackoverflow.com/a/6680767/793796). I got the windows binaries of required commands, but I cannot tell, if first command has failed. – anishsane Dec 27 '13 at 07:41
  • +1 This should be the normal solution, as it's simple and easily understandable – jeb Dec 27 '13 at 08:58
2

I don't believe, that there is a direct way for this, but you could write a helper batch file for this.

pipe.bat

@echo off
set pipeNo=%1
shift
call %1 %2 %3 %4 %5 %6 %7 %8 %9
> piperesult%pipeNo%.tmp echo %errorlevel%

Then you could call it via

pipe 1 command1 | pipe 2 command2 | pipe 3 command3

This creates three files piperesult<n>.txt with the result for the commands.

jeb
  • 78,592
  • 17
  • 171
  • 225
  • Accepting this solution, because this seems to solve my requirement.. Thanks. – anishsane Dec 27 '13 at 09:03
  • +1, but script will not work if command is a batch file. A pipeCall script that uses CALL is required, or else a single pipe script that uses/does not use CALL, depending on presence or absence of a /CALL option. – dbenham Dec 27 '13 at 12:49
  • @dbenham Good point, and simple to solve by using always `CALL`. Btw. Do you see a possibility for auto numbering the pipeResults? – jeb Dec 27 '13 at 12:52