Page 1 of 1

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

Posted: 30 Nov 2011 22:51
by Ed Dyreen
'
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 :?:

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

Posted: 01 Dec 2011 01:50
by Judago
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

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

Posted: 01 Dec 2011 02:36
by walid2mi
*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

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

Posted: 01 Dec 2011 03:37
by jeb
You could also try a look at SO:Redirection

jeb

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

Posted: 02 Dec 2011 12:16
by jeb

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

Posted: 02 Dec 2011 12:31
by aGerman
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

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

Posted: 02 Dec 2011 16:13
by Ed Dyreen
:P
Thanks everyone, once again I'm impressed so much, it is now bookmarked.