Work the Shell - Handling Errors and Making Scripts Bulletproof
Finally, another thing to be aware of with the shell is that it's all too easy to zap important files with a redirect. For example, this shouldn't work:
$ who > who.output $ ls > who.output
The second command should generate an error because the output file already exists, right? But it doesn't, and it simply trashes the who output without a warning or error—not good.
To avoid that problem, you'll want to set -o noclobber in scripts or, better, for your login shell, and let it be inherited by subshells, including those that run your shell scripts. A good place to put it could be in your .profile or .bashrc.
With noclobber set, the two commands behave differently:
$ ls > who.output -bash: who.output: cannot overwrite existing file
That's useful for everyone, and doubly so for us shell script hackers, right?
Dave Taylor is a 26-year veteran of UNIX, creator of The Elm Mail System, and most recently author of both the best-selling Wicked Cool Shell Scripts and Teach Yourself Unix in 24 Hours, among his 16 technical books. His main Web site is at www.intuitive.com, and he also offers up tech support at AskDaveTaylor.com. Follow him on Twitter if you'd like: twitter.com/DaveTaylor.
Dave Taylor has been hacking shell scripts for over thirty years. Really. He's the author of the popular "Wicked Cool Shell Scripts" and can be found on Twitter as @DaveTaylor and more generally at www.DaveTaylorOnline.com.
- Two Pi R
- Readers' Choice Awards 2013
- AIDE—Developing for Android on Android
- A Handy U-Boot Trick
- The Geek's Guide to the Coolest 2013 Holiday Gifts
- Best. Cake. Ever.
- Sublime Text: One Editor to Rule Them All?
- Raspberry Pi: the Perfect Home Server
- Tech Tip: Really Simple HTTP Server with Python
- RSS Feeds
- Reality is disapointing
10 hours 2 min ago
- Máy sấy quần áo
12 hours 48 min ago
- Services on GlusterFS
12 hours 58 min ago
- Reply to comment | Linux Journal
14 hours 38 min ago
- Definitely cool stuff here
15 hours 39 min ago
- thanks for the information
16 hours 50 min ago
- nice information thanks
17 hours 29 min ago
20 hours 25 min ago
- The lost opportunity of security
1 day 8 hours ago
- Andhra Pradesh has a variety
1 day 12 hours ago