[buug] RE: I/O redirection
Michael Paoli
mp at rawbw.com
Wed Jun 9 07:21:10 PDT 2004
Anyway, last BUUG meeting, question was brought up about I/O
redirection. I thought I had something where I'd covered this
before.
Anyway, with a wee edit of command/argument(s) and dropping
out some names/e-mail addresses, ... here's item from fairly
recently where I addressed a bit of that (it does addresses
fair bit of what tends to be a fairly reccurrent
class of question(s) / area(s) of confusion).
Actually, where I mention:
"
The command could also be written:
0</dev/console 1>/dev/console 2>&1 command [argument ...]
among other possibilities, and still be equivalent.
"
That can be rearranged fairly arbitrarily, specifically, each of these
components:
0</dev/console
1>/dev/console
2>&1
command [argument ...]
can be rearranged in any order, as long as 2>&1 occurs subsequent to
1>/dev/console
Also, < defaults to 0< and > defaults to 1>, so the 0 and 1 immediately
before < and > respectively, can be dropped.
> -----Original Message-----
> From: Paoli, Michael
> Sent: Wednesday, April 21, 2004 6:14 PM
> Subject: RE: Are These Equivalent?
>
> Yes, they're equivalent.
>
> I/O redirection is essentially processed left to right (and prior to
> command execution),
>
> Here we have command and arguments:
command [argument ...]
> And then I/O redirection:
> A) > /dev/console 2>&1 < /dev/console
> B) < /dev/console > /dev/console 2>&1
> In A, first we redirect (default file descriptor (fd) 1 (stdout) for >,
> since > isn't immediately preceded by a digit) to /dev/console (or more
> specifically, open /dev/console for writing with truncation, and use
> copy/assign that for fd 1),
> then (2>&1) we copy file descriptor 1 (which we previously set for
> /dev/console) to file descriptor 2 (stderr), so /dev/console now also
> becomes stderr, and then (< /dev/console) we open /dev/console for reading
> and use it for fd 0 (fd 0 (stdin) is default for < if not immediately
> preceded by a digit). So we end up with /dev/console for stdin, stdout,
> and stderr, and opened for write/truncation
>
> In B, we redirect fd 0 from /dev/console, redirect fd 1 to /dev/console,
> and copy fd 1 to fd 2, so the net result is the same (/dev/console used
> for all three, stdout/stderr /dev/console opened for write/truncated, and
> stdin /dev/console for read)
>
> The command could also be written:
> 0</dev/console 1>/dev/console 2>&1 command [argument ...]
> among other possibilities, and still be equivalent.
>
> Note, however, that typically these are not equivalent:
> X) 2>&1 >/dev/console
> Y) >/dev/console 2>&1
>
> Y results in fd 1 and fd 2 using /dev/console,
> X, however results in fd 1 using /dev/console, but fd 2 ends up using
> whatever fd 1 was before the ">/dev/console" I/O redirection (so stderr
> may not necessarily at all end up written to /dev/console).
>
> A sequence like:
> 1>&2 echo "$0: failure reason/condition/program location, aborting"; exit
> 1
> may be relatively common - echo by default writes to stdout, however by
> using I/O redirection to copy fd 2 (stderr) to fd 1 (stdout) before the
> echo command is executed, echo ends up effectively writing to stderr (echo
> still thinks it's writing stdout, but prior to the echo, we made stdout go
> the same place that stderr would go).
>
>
>
> -----Original Message-----
> Sent: Wednesday, April 21, 2004 4:25 PM
> To: Paoli, Michael
> Subject: Are These Equivalent?
>
> Hi, Michael--
>
> Another quick (I hope) question: Do these mean the same thing?
>
command [argument ...] > /dev/console 2>&1 < /dev/console
command [argument ...] < /dev/console > /dev/console 2>&1
> Thanks,
----- End forwarded message -----
More information about the buug
mailing list