On Tuesday 21 June 2011 09:42:10 Dominic Ringuet wrote: > On Tue, Jun 21, 2011 at 8:22 AM, Webmaster <[email protected]> wrote: > > For example: > > ls -l /usr/bin | more > redirect_test.log 2>&1 (BLFS 6.3 Page 37) > > What does "2>&1" mean? > > 2 is stderr > > > is a redirector > > &1 is stdout > > Without it, both normal output and errors will be printed in the file; > with it, normal output goes in the file and error is on the screen. > (unless standard output goes somewhere else) > > Examples: > > ... > > So far, there are no errors, the behavior is the same in both > instances. The expression becomes meaningful only if there is an > error: > > ls -al /non/existant/directory > test.log 2>&1 > * will print "No such file or directory" on screen, leaving test.log empty > > ls -al /non/existant/directory > test.log > * will print "No such file or directory" in test.log
Horseshoes and hand grenades. Your first 'error' example will place STDOUT and STDERR in test.log. Your second example will display the error message on the terminal and leave the log file empty. The general form 'X>&Y' means "close file descriptor (FD) X, then make it a duplicate of file descriptor Y". The order is important. The commands command 2>&1 >/dev/null and command >/dev/null 2>&1 result in very different output. The first closes file descriptor (FD) 2 then makes it a duplicate of FD 1, then closes FD 1 and opens it on /dev/null; the result is that the program's STDERR goes to wherever STDOUT was pointing, and STDOUT is dumped into the bit bucket. The second closes FD 1 and opens it on /dev/null, then closes FD 2 and makes it a duplicate of FD 1; the result is that the program's STDOUT and STDERR are both dumped in the bit bucket. You can even swap STDOUT and STDERR: command 3>&1 1>&2 2>&3 3>&- Remember that computers are dumb: they do things in the order they're told. -- http://linuxfromscratch.org/mailman/listinfo/lfs-support FAQ: http://www.linuxfromscratch.org/lfs/faq.html Unsubscribe: See the above information page
