[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: 2.4.0 on cygwin - Bad fd number
From: |
Karl Hammar |
Subject: |
Re: 2.4.0 on cygwin - Bad fd number |
Date: |
Sat, 06 Nov 2004 10:55:24 +0100 |
Han-Wen Nienhuys <address@hidden> wrote:
> address@hidden writes:
...
> > Error invoking `latex \\nonstopmode \\input simple.tex 2>&1 1>&
> > /dev/null '. Return value 512Error invoking `dvips -t a4
> > -u+ec-mftrace.map -u+lilypond.map -Ppdf simple 2>&1 1>& /dev/null '.
> > Return value 512
>
> Ah wait, the shell is struggling with the redirection syntax.
>
> are there any unix gurus that know the correct syntax for redirecting
> all output to /dev/null?
Short story:
$ ... > /dev/null 2>&1
or
$ ... > /dev/null 2> /dev/null
Long story:
. echo writes to stdout (file descriptor 1)
. redirection "$cmd a> file" is:
(common case: a==1 || a==2)
close(a); open("file",O_WRONLY) /* == a */;
. to make it write to stderr (fd 2), dup2(b,a) it by "$cmd a>&b"
. you can omit the "1" in 1>... below, because it is the default number
$ (echo output; echo error 1>&2 )
output
error
$
Example A
=============
Example below works since:
1 close(1); open("a",O_WRONLY) /* == 1 */;
2 fork() and exec() subshell /* remember file descriptor table is inherited */
3 run echo sequentially
$ (echo output; echo error 1>&2 ) 1> a
error
$ cat a
output
$
Example B
=============
$ (echo output; echo error 1>&2 ) 2> b
output
$ cat b
error
$
Example C
=============
close(1); open("c",O_WRONLY) /* == 1 */; dup2(1,2);
$ (echo output; echo error 1>&2 ) 1> c 2>&1
$ cat c
output
error
$
Example D
=============
This one is equivalent to the above, but less to type
it is a special syntax to redirect both stdout and stderr
(you could also use " >& d")
$ (echo output; echo error 1>&2 ) &> d
$ cat d
output
error
$
The drawback is that not all shells accepts it,
e.g. ash treats it like
. a "(echo output; echo error 1>&2 ) &" command
. followed by a commandless redirection "> d", which empties the file
$ ash
$ (echo output; echo error 1>&2 ) &> d
$ output
error
press return to get a new prompt
[1] + Done (echo output; echo error 1>&2)
$ cat d
$
Example E
=============
Redirection is order dependant, this one does:
dup2(1,2); close(1); open("e",O_WRONLY) /* == 1 */;
$ (echo output; echo error 1>&2 ) 2>&1 1> e
error
$ cat e
output
$
Example F
=============
And yes, you can bypass it (/dev/tty is "current" tty)
$ (echo output; (echo wall > /dev/tty; echo error) 1>&2 ) &> f
wall
$ cat f
output
error
$
Example G
=============
So:
$ (echo output; echo error 1>&2 ) 2>&1 1> g
error
$ cat g
output
$
will become:
dup2(1,2); /* all's well, stderr will go to current stdout (/dev/tty) */
close(1); open("g",O_WRONLY) /* == 1 */;
Example H
=============
And lastly, don't use 1>&,
bash accepts it and treats it like >&, but
ash says it's a syntax error
$ (echo output; echo error 1>&2 ) 2>&1 1>& h
$ cat h
output
error
$ ash
$ (echo output; echo error 1>&2 ) 2>&1 1>& h
ash: Syntax error: Bad fd number
$
Regards
/Karl