HELP: 2>&1 > /dev/null != 2>&- > /dev/null ???

HELP: 2>&1 > /dev/null != 2>&- > /dev/null ???

Post by Larry Ch » Thu, 03 Jun 1993 01:15:57



In /bin/sh, I tried to redirect the standard error and standard output to
/dev/null.

I still has the error message from the following command:

        ls /xyz 2>&1 > /dev/null

but not from the following command:

        ls /xyz 2>&- > /dev/null

I thought >&1 is the same as >&-. Any comments are appreciated.

Thank you.

Larry

 
 
 

HELP: 2>&1 > /dev/null != 2>&- > /dev/null ???

Post by Jim Dav » Thu, 03 Jun 1993 03:07:55



>In /bin/sh, I tried to redirect the standard error and standard output to
>/dev/null.

>I still has the error message from the following command:

>    ls /xyz 2>&1 > /dev/null

>but not from the following command:

>    ls /xyz 2>&- > /dev/null

>I thought >&1 is the same as >&-. Any comments are appreciated.

'>&-' closes stdout.  Given

$ ls /etc/motd /etc/nonesuch
/etc/nonesuch not found
/etc/motd

you can throw away stderr by redirecting it to /dev/null,

$ ls /etc/motd /etc/nonesuch 2>/dev/null
/etc/motd

or throw away stdout by redirecting it to /dev/null,

$ ls /etc/motd /etc/nonesuch >/dev/null
/etc/nonesuch not found

or throw away both by redirecting stdout to /dev/null, and dup'ing
stderr to stdout.

$ ls /etc/motd /etc/nonesuch >/dev/null 2>&1

Note the difference between that command and

$ ls /etc/motd /etc/nonesuch 2>&1 >/dev/null
/etc/nonesuch not found

which dups stderr to stdout (probably the terminal), and then throws
away stdout.
--
Jim Davis               | "You mean about your close personal relationship


 
 
 

HELP: 2>&1 > /dev/null != 2>&- > /dev/null ???

Post by Frank Pete » Thu, 03 Jun 1993 03:57:00



:
: In /bin/sh, I tried to redirect the standard error and standard output to
: /dev/null.
:
: I still has the error message from the following command:
:
:       ls /xyz 2>&1 > /dev/null

Order counts here.  If you redirect standard error to the same place as
standard output and *then* redirect standard output then standard error
won't follow it.  At the time you redirect standard error, standard
output is still pointed at the terminal (presumably).  When you later
redirect standard output to /dev/null standard error is left pointing
to the terminal.

What you intended, I think, is:

     ls /xyz > /dev/null 2>&1

Which redirects standard output to /dev/null and *then* sends standard
error after it.

: but not from the following command:
:      
:       ls /xyz 2>&- > /dev/null
:
: I thought >&1 is the same as >&-. Any comments are appreciated.

No, >&- closes the descriptor.  This command directs standard output to
/dev/null and closes standard error completely.  It has much the same
effect (no output or error messages) but is logically completely
different.
--
Frank Peters  -  UNIX Systems Programmer  -  Mississippi State University

 
 
 

HELP: 2>&1 > /dev/null != 2>&- > /dev/null ???

Post by Alan Wats » Thu, 03 Jun 1993 03:13:35



>I still has the error message from the following command:

>    ls /xyz 2>&1 > /dev/null

>but not from the following command:

>    ls /xyz 2>&- > /dev/null

>I thought >&1 is the same as >&-. Any comments are appreciated.

RTFM.

(a) Redirections take effect from left to right, thus:

        foo 2>&1 >/dev/null

makes stderr a copy of stdout (which probably writes to the tty)
and then redirects stdout to /dev/null, whereas

        foo >/dev/null 2>&1

redirects stdout to /dev/null, and the makes stderr a copy of stdout
(so that stderr also writes to /dev/null).

(b) X>&- means close file descriptor X, whereas X>&1 means make file
descriptor X a copy of file descriptor 1 (stdout).

 
 
 

HELP: 2>&1 > /dev/null != 2>&- > /dev/null ???

Post by Barry Margol » Thu, 03 Jun 1993 08:35:08



>In /bin/sh, I tried to redirect the standard error and standard output to
>/dev/null.

>I still has the error message from the following command:

>    ls /xyz 2>&1 > /dev/null

That should be:

        ls /xyz >/dev/null 2>&1

From the sh(1) man page:

     The order in which redirections are  specified  is  signifi-
     cant.   The shell evaluates redirections left-to-right.

Therefore, the problem with your version is that it first copies file
descriptor 1 (stdout) to fd 2 (stderr), and then changes fd 1.  Fd 2 is
still referring to the file that fd 1 originally referred to (the
terminal).

Quote:>but not from the following command:

>    ls /xyz 2>&- > /dev/null

>I thought >&1 is the same as >&-. Any comments are appreciated.

">&-" closes the specified file descriptor, rather than copying another
file descriptor.  There's a difference between a closed file and an open
file connected to /dev/null.
--
Barry Margolin
System Manager, Thinking Machines Corp.


 
 
 

HELP: 2>&1 > /dev/null != 2>&- > /dev/null ???

Post by Tom Hofma » Thu, 03 Jun 1993 15:43:43



>">&-" closes the specified file descriptor, rather than copying another
>file descriptor.  There's a difference between a closed file and an open
>file connected to /dev/null.

What is that difference?

---Tom

 
 
 

HELP: 2>&1 > /dev/null != 2>&- > /dev/null ???

Post by Chet Ram » Thu, 03 Jun 1993 22:20:13



>>There's a difference between a closed file and an open
>>file connected to /dev/null.

>What is that difference?

Writes to a closed file descriptor fail, as do dup2() and the various
fcntl() calls.  Writes to a file descriptor open to /dev/null simply
disappear, and all the other calls work.  It's the difference between
valid and invalid file descriptors.

Chet

--
        ``Times are hard, and you're afraid to pay the fee.
          So you find yourself somebody who can do the job for free.''


 
 
 

1. rsh machine -n 'command >&/dev/null </dev/null &'

My copy of the Unix FAQ suggests syntax like this

            rsh machine -n 'command >&/dev/null </dev/null &'

for running rsh (assuming I use csh on the remote machine, and
something similar if I use sh remotely).  This has always worked
fine for me under SunOS 4.1.2.

My question is: Will this magic necessary under Solaris 2.X?

Many thanks!
---
------------------------------------------------------------------
Francois-Michel Lang          (202) 752-6067   FAX: (202) 752-5074


2. ld.so.cache has wrong version number

3. setting 2>&1 >>/dev/null for a chunk of script

4. International characters in X

5. Nowhere Man >> Unix Man 2>/dev/null :-)

6. Check your ppp.log or ppp.tun0.log in /var/log

7. Test > &> /dev/null

8. lm sensor trouble

9. BOURNE - exec >/dev/null 2>&1 wierdness

10. Please explain ">/dev/null 2>&1"

11. 2>&1 |tee && 2>&1 >out

12. LILO help <><><><><><>

13. Why does SunOS crash for '(cmd &) >& /dev/null'?