argument precedence, output redirection

2010-12-03 Thread Payam Poursaied

Hi all,
I'm not sure this is a bug or please let me know the concept:
What is the difference between:
ls  -R /etc/ 2>&1 1>/dev/null
and
ls -R /etc/ 1>/dev/null 2>&1
 
the second one redirect everything to /dev/null but the first one, still
prints errors (run as a non root user would unveil the problem)
it the order of arguments important? If yes, what is the idea/concept behind
this behavior?

Best Regards



smime.p7s
Description: S/MIME cryptographic signature


Re: argument precedence, output redirection

2010-12-03 Thread Greg Wooledge
On Fri, Dec 03, 2010 at 06:16:06PM +0330, Payam Poursaied wrote:
> What is the difference between:
> ls  -R /etc/ 2>&1 1>/dev/null
> and
> ls -R /etc/ 1>/dev/null 2>&1

http://mywiki.wooledge.org/BashFAQ/055

In a sentence, redirections are done in order, from left to right.



Re: argument precedence, output redirection

2010-12-03 Thread Eric Blake
On 12/03/2010 07:46 AM, Payam Poursaied wrote:
> 
> Hi all,
> I'm not sure this is a bug or please let me know the concept:
> What is the difference between:
> ls  -R /etc/ 2>&1 1>/dev/null
> and
> ls -R /etc/ 1>/dev/null 2>&1

POSIX requires that redirections are evaluated from left to right.

The first line duplicates fd 2 from 1 (that is, stderr is now shared
with stdout), then changes fd 1 onto /dev/null (so you've silenced
stdout, and errors from ls will show up on your stderr).

The second line changes fd 1 onto /dev/null, then duplicates fd 2 from 1
(that is, stderr is now shared with /dev/null, and you've silenced all
output to either stream).

> the second one redirect everything to /dev/null but the first one, still
> prints errors (run as a non root user would unveil the problem)
> it the order of arguments important? If yes, what is the idea/concept behind
> this behavior?

Yes the order is important, and the idea behind the behavior is that
left-to-right evaluation order can be easily documented and relied on.

-- 
Eric Blake   ebl...@redhat.com+1-801-349-2682
Libvirt virtualization library http://libvirt.org



signature.asc
Description: OpenPGP digital signature


Re: argument precedence, output redirection

2010-12-03 Thread Chet Ramey
> I'm not sure this is a bug or please let me know the concept:
> What is the difference between:
> ls  -R /etc/ 2>&1 1>/dev/null
> and
> ls -R /etc/ 1>/dev/null 2>&1
>  
> the second one redirect everything to /dev/null but the first one, still
> prints errors (run as a non root user would unveil the problem)
> it the order of arguments important? If yes, what is the idea/concept behind
> this behavior?

Redirections are processed left-to-right (or, if you prefer, beginning to end).
The manual page has this to say:

   Note  that  the order of redirections is significant.  For example, the
   command

  ls > dirlist 2>&1

   directs both standard output and standard error to  the  file  dirlist,
   while the command

  ls 2>&1 > dirlist

   directs  only the standard output to file dirlist, because the standard
   error was duplicated as standard output before the standard output  was
   redirected to dirlist.

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/



Re: argument precedence, output redirection

2010-12-03 Thread Marc Herbert
Le 03/12/2010 14:46, Payam Poursaied a écrit :
> 
> Hi all,
> I'm not sure this is a bug or please let me know the concept:
> What is the difference between:
> ls  -R /etc/ 2>&1 1>/dev/null
> and
> ls -R /etc/ 1>/dev/null 2>&1
>  
> the second one redirect everything to /dev/null but the first one, still
> prints errors (run as a non root user would unveil the problem)
> it the order of arguments important? If yes, what is the idea/concept behind
> this behavior?

I was confused by this for a long time and kept reading again and
again the correct but *too long* answers others have posted, until I
started using this *short* reading trick:


  2>&11>/dev/null

   output2 := output1;  output1 := NULL


When you apply this from left to right it should be quite obvious
why output2 is not NULL.


And now you can finally understand things like this:

exec 3>&1  4>&2# save
exec 1>/dev/null 2>&1

#   discard everything ...

exec 1>&3  2>&4# restore






argument precedence, output redirection

2010-12-03 Thread Payam Poursaied
Hi all,
I'm not sure this is a bug or please let me know the concept:
What is the difference between:
ls  -R /etc/ 2>&1 1>/dev/null
and
ls -R /etc/ 1>/dev/null 2>&1
 
the second one redirect everything to /dev/null but the first one, still
prints errors (run as a non root user would unveil the problem)
it the order of arguments important? If yes, what is the idea/concept behind
this behavior?

Best Regards



smime.p7s
Description: S/MIME cryptographic signature