redirection >&1 >&2 >&3 >&4

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

redirection >&1 >&2 >&3 >&4

#1 Post by Ed Dyreen » 30 Nov 2011 22:51

'
From viewtopic.php?f=3&t=2520
alan_b wrote:Many thanks. Never used user-defined streams before.
I only knew that >&1 >&2 and >&3 were related to StdOut, StdErr and StdIn
I only knew
>&1 was connected to ? ( echo.hello >&1 don't work )
>&2 was connected to StdErr
that con takes both 1 & 2 and nul is nothing.

I don't understand the >&1 redirection, it doesn't seems to behave the same, in fact it doesn't seem to work at all :?:

Judago
Posts: 15
Joined: 04 Nov 2011 07:59

Re: redirection >&1 >&2 >&3 >&4

#2 Post by Judago » 01 Dec 2011 01:50

Actually it's:

0 == StdIn
1 == Stdout
2 == StdErr

The reason ">&1" doesn't work is because output redirection defaults from StdOut unless explicitly specified; ">&1" is equivalent to "1>&1". So basically you're trying to redirect a stream to itself.

I don't know what 3 - 7 do but I found a strange bug a while ago using "3".

Code: Select all

rem ONLY works from an open cmd window, not scripts
rem you will also probably need to close the window afterwards
cmd<nul 3>nul

walid2mi
Posts: 3
Joined: 01 Dec 2011 02:10
Contact:

Re: redirection >&1 >&2 >&3 >&4

#3 Post by walid2mi » 01 Dec 2011 02:36

*google translation*

Code: Select all

>3 and >&3


is used in persistent redirects

their use can extend the ability of command scripts:

1- creation of a special flow to capture all console output

2- have a more flexible management of errors and output STDOUT

..etc

jeb
Expert
Posts: 1043
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: redirection >&1 >&2 >&3 >&4

#4 Post by jeb » 01 Dec 2011 03:37

You could also try a look at SO:Redirection

jeb

jeb
Expert
Posts: 1043
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: redirection >&1 >&2 >&3 >&4

#5 Post by jeb » 02 Dec 2011 12:16


aGerman
Expert
Posts: 4656
Joined: 22 Jan 2010 18:01
Location: Germany

Re: redirection >&1 >&2 >&3 >&4

#6 Post by aGerman » 02 Dec 2011 12:31

There seem to be 4 predefined streams
0 stdIn
1 stdOut
2 stdErr
3 incoming file stream
____________________________________________________

You could display the stream numbers 0 and 1
*.bat

Code: Select all

@prompt $g$s
>xyz.txt echo(
<xyz.txt more +1
@echo off
del xyz.txt
pause>nul

Output:

Code: Select all

> echo( 1>xyz.txt

> more +1 0<xyz.txt

You will find the 1 in the first line and 0 in the second line even if they were not parts of the code.
____________________________________________________

You could redirect other streams to stdOut.
dir :
... produces two different streams at the same time: stdOut and stdErr (where Datei nicht gefunden ~> file not found)
Note that only stdOut can be piped (|) to another command...
*.bat

Code: Select all

@prompt $g$s &cd /d C:\

@echo This is the line I'd like to play with:
dir :
@echo(
@pause
@echo(--------------------------------------------
dir : |findstr .
@echo(
@pause
@echo(--------------------------------------------
dir : 2>&1 |findstr .
@echo(
@pause
@echo(--------------------------------------------
dir : 2>&1 >nul |findstr .
@echo(
@pause

Output:

Code: Select all

This is the line I'd like to play with:

>  dir :
 Volume in Laufwerk C: hat keine Bezeichnung.
 Volumeseriennummer: 9EA9-0A00

 Verzeichnis von C:\

Datei nicht gefunden

Drücken Sie eine beliebige Taste . . .
--------------------------------------------

>  dir :   | findstr .
Datei nicht gefunden
 Volume in Laufwerk C: hat keine Bezeichnung.
 Volumeseriennummer: 9EA9-0A00
 Verzeichnis von C:\

Drücken Sie eine beliebige Taste . . .
--------------------------------------------

>  dir :   2>&1  | findstr .
 Volume in Laufwerk C: hat keine Bezeichnung.
 Volumeseriennummer: 9EA9-0A00
 Verzeichnis von C:\
Datei nicht gefunden

Drücken Sie eine beliebige Taste . . .
--------------------------------------------

>  dir :   2>&1 1>nul  | findstr .
Datei nicht gefunden

Drücken Sie eine beliebige Taste . . .

The first section shows the entire output.
The 2nd section displays the stdErr first while the stdOut is piped to FINDSTR and appears later.
In the 3rd section stdOut and stdErr are merged and both are piped to FINDSTR (that removes the blank lines).
The 4th section is a bit tricky to understand. It's written opposite, but the redirections are processed:
- first stdOut to NUL
- then stdErr to stdOut
As you can see the former stdErr is now the stdOut and can be piped to FINDSTR.
____________________________________________________

You can redirect to stream 3 only if it was not used. Otherwise an error occurs.

dummy.txt

Code: Select all

1st line in dummy.txt

test.bat

Code: Select all

@echo off
echo(>&3
set /p "var=file input?"
echo("%var%"
echo ------------------------------------------------------

called from the command prompt

Code: Select all

> rem Without file redirection:

> test.bat

file input?
""
------------------------------------------------------

> rem With file redirection:

> test.bat<dummy.txt
Das System kann nicht auf das angegebene Gerät schreiben.
file input?"1st line in dummy.txt"
------------------------------------------------------

>

... where Das System kann nicht auf das angegebene Gerät schreiben. ~> The system cannot write to the specified device.

Hope that helps.

Regards
aGerman

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: redirection >&1 >&2 >&3 >&4

#7 Post by Ed Dyreen » 02 Dec 2011 16:13

:P
Thanks everyone, once again I'm impressed so much, it is now bookmarked.

Post Reply