| By Dave Taylor | Article Rating: |
|
| September 27, 2004 12:00 AM EDT | Reads: |
12,519 |
By this point in our discussion you should be comfortable with the idea of the three file descriptors associated with the Linux command line: stdin, stdout, and stderr (pronounced "standard in," "standard out," and "standard error"). If you're using a descendant of the Bourne Shell (such as Bash), you also learned last month how to use the x>&y notation to redirect the different descriptors from the command line, and therefore from within a script too. This time I'll turn my attention to the C shell with its different command-line syntax.
My focus here is ultimately on shell scripting, so I have to point out that the vast majority of Linux shell scripts are written in a Bourne Shell-compatible scripting environment, whether it's directly in /bin/sh or in a more sophisticated environment like Bash. It's not that Csh and Ksh aren't good interactive shells; it's just that diversity breeds incompatibility and confusion rather than any sort of long-term advantage from the scripter's perspective.
You'll have to make your own decision on what shell forms the basis of your scripting environment, but my strong recommendation is to work with Bash or a similar shell. In future columns, I won't spend much time at all on C Shell scripting methods and structures, so if you think that's a major problem, please e-mail me and let me know.
Redirecting Stderr Within Csh
There are couple of basic redirections that you'll want to know how to accomplish within the Csh environment, the most important of which is to redirect stderr (standard error) to the same place as stdout (standard out). This is perhaps the easiest to accomplish:wc -l does-not-exist >& my.output
This notation - the added "&" after the redirect symbol - causes stderr to be mapped to the same place as stdout, that is, to the file "my.output".
One problem that might occur is that the output file may already exist and you may have wisely set "noclobber" (that is, included the line "set noclobber" in your .cshrc) to try and minimize errors with overwritten files. The general notation for forcing an overwrite in this situation is to add an exclamation mark:
wc -l does-not-exist >! existing-output-file
We can slip in an ampersand and have a redirect that overrides "noclobber" and sends both stdout and stderr to the file all at once:
wc -l missing >&! existing-output-file
Personally, I find this a bit confusing because of what us geeks call function overloading: at the end of a line, the "&" means that the job should be run in the background, and followed by a number or pattern; "!" is a reference to the previous command history in Csh. Hopefully your brain can untangle all of this more easily.
Did I mention that you could append output to files just as easily with these redirects? Simply use ">>" where you'd otherwise use ">", as in:
wc -l missing >>&! existing-output-file
However, if that doesn't look like the cat jumping on the keyboard, I don't know what will.
Unlike Bash, you cannot split out stderr in Csh to route it elsewhere, so it's not as flexible a mechanism. Your choices are either to have stderr go to the console, the default behavior, or have it duplicate the destination of stdout.
Within a pipe, you can map stderr to stdout just as you can in a file redirect, and the notation should be logical and no surprise:
wc -l somefile |& grep -i error
This command tries to count the number of lines in somefile, sending both stdout and stderr to the grep command via a pipe (without the "&" in the pipe notation, of course, stderr would go to the screen and grep would never see it. A common mistake for neophyte scripters). The grep command then simply looks for the word "error." The net result of this is that if some file exists and wc can correctly count, there's no output, but if there's an error, the error message is displayed.
Published September 27, 2004 Reads 12,519
Copyright © 2004 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Dave Taylor
Dave Taylor, a contributing editor to Linux.SYS-CON.com, has been involved with the Linux and Unix community since 1980 and has written a number of best-selling Unix books. Currently, he writes, teaches, and works as a management consultant to tech startups, along with his new venture, Ask Dave Taylor!, www.askdavetaylor.com and his personal blog is www.blog.Linux.SYS-CON.com. To contact Dave, please go to /www.intuitive.com/contact.shtml.
- Kindle 2 vs Nook
- Is Cloud Computing Like Teenage Sex?
- GovIT Expo Highlights Cloud Computing
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Cloud Computing Can Revitalize Your Career as Software Developer
- Ubuntu-based Open Source Linux Mint Tests KDE Version
- Yahoo! SVP Shelton Shugar to Discuss Innovation at Cloud Computing Expo
- Virtualization Journal "Readers' Choice Awards" Voting Is Now Open
- Einstein, Sharks and Clouds: IT Security in the Cloud
- Adobe Flex Developer Earns $100K in New York City
- Virtualization Expo Call for Papers Deadline December 15
- Amazon Web Services Database in the Cloud
- Kindle 2 vs Nook
- Cloud CEOs, CTOs & SVPs to Speak at 4th International Cloud Computing Expo
- Is Cloud Computing Like Teenage Sex?
- 1st Annual GovIT Expo: Letter from the Technical Chair
- Ulitzer News: Search vs New Media
- The Difference Between Web Hosting and Cloud Computing
- Cloud Computing Expo: Exclusive Q&A with Yahoo! SVP Cloud Computing
- Confessions of a Ulitzer Addict
- GovIT Expo Highlights Cloud Computing
- Twitter, Linked In, Ning and Ulitzer: Easy Personal Branding Strategy
- My Thoughts on Ulitzer
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- The i-Technology Right Stuff
- Linux.SYS-CON.com Exclusive: Linus Discloses *Real* Fathers of Linux
- After Ubuntu, Windows Looks Increasingly Bad, Increasingly Archaic, Increasingly Unfriendly
- Linus' Top Ten SCO Barbs
- A Closer Look at Damn Small Linux
- Netscape Co-Founder's 12 Reasons for Growth of Open Source
- Introducing "Cooperative Linux" - Linux for Windows, No Less
- *POINT - COUNTERPOINT SPECIAL* What's Wrong with the Open Source Community?
- Where Are RIA Technologies Headed in 2008?
- Linux.SYS-CON.com Exclusive: What Would UserLinux Look Like?
- i-Technology Viewpoint: The New Paradigm of IT Buying
- Is Linux Desktop-Ready Yet...or Not?































