2003/12/29 2>&1

Shell redirection, and the "2>&1" is probably the most common form. That simply means to send stderr to wherever stdout is pointing. An easy way to demonstrate all this is to create a direcory and put one file in it:



mkdir tt
cd tt
touch t



Hate these ads?

That gives us a known situation to work with. Sitting in that directory,



ls foo t > y


Puts "t" in "y", but displays "ls: foo: No such file or directory" on the screen. That's because ls writes what it doesn't understand to stderr (file descriptor 2). If we want both things in y, we just do:



 ls foo t >y  2>&1 


There are lots of ways to do this sort of thing. More interesting is the case of a shell script where you want to capture stderr but let any "ok" output go to the screen. To do that, take advantage of other file descriptors:



x=`ls foo t 3>&2 2>&1 1>&3`







$x will have the error message but "t" will go to the screen. How does that happen? Well, 3>&2 puts stderr into (normally unused) file descriptor 3. Then 2>&1 puts 1 into 2. This might be hard for you to follow, but hang in there a minute. Next, 1>&3 puts 3 (which is the original 2) into 1. Effectively, this reverses stdout and stderr. Let's take a closer look:

When the shell starts , both stderr (1) and stdout (1) point to our screen. However, the



x=`ls foo t `


Would leave stderr still at the screen, but stdout would redirect to our "x" variable. We want the opposite of that: The 3>&2 makes 3 the same as 2, so now 3 is going to the screen. 2>&1 makes 2 go where 1 was, which is into our "x". Finally, 1>&3 puts 1 back to the screen by copying 3.

(See exec also)



Comments /Words/2003_12_29.html


Add your comments
cartoon
Forget the expense of flying to New England. Forget hotel and meals costs.
Installation and light training Boston and New England


Enter your email address for automatic notification of new posts here
(be sure to whitelist 'feedburner.com' if you use spam filtering)

Or use any RSS reader

Delivered by FeedBurner


Views for this page
Today This Week This Month This Year  Overall
10101083,681 14,779

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

Publishing your articles here

pavatar.jpg
More:




Unix/Linux Consultants

Your ad here - $24.00 yearly!

SCO, OpenServer, UnixWare, software, servers, security, networks, installation, administration, troubleshooting, maintenance, Watchguard, firewalls, VPNs, e-mail. Visit us at http://opensystemscomputing.com and www.go2unix.com.


http://www.cleverminds.net Need expert advice? Want a second opinion? CleverMinds is a one-stop-shop for a wide range of technology solutions. We support Unix, Linux, SCO as well as CMS, ecom, blogs, podcasts, search engines consulting and more. Contact us at web2.0@cleverminds.net 0r (617) 894-1282


http://www.loch-raven.com/Loch Raven provides consulting services for Linux, UNIX, SCO OpenServer, SCO UnixWare, AIX, Solaris and FreeBSD systems. Services include: System administration, Backup and Disaster Recovery planning and implementation, Custom Shell Scripting, System Recovery, Remote Administration, Secure Remote Access configuration, Troubleshooting, System Migration and Windows/Linux/Unix integration with Samba. Loch Raven also provides web site hosting, solutions, and consulting along with Joomla CMS.









Change Congress


Related Posts