anishsane
anishsane

Reputation: 20980

Batch file equivalent of PIPESTATUS

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?

Upvotes: 2

Views: 924

Answers (2)

jeb
jeb

Reputation: 82390

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.

Upvotes: 2

Magoo
Magoo

Reputation: 80138

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%

Upvotes: 3

Related Questions