Trap Shell-Script Errors
An easy way to protect shell scripts from creating havoc when they go wrong due to a missing directory is to replace the shell's cd command by a function declared at the start of the script:
cd(){
if ! builtin cd $1
then echo "Failed to cd $1 !!!" >&2
exit 1
fi
}
No other changes are needed in the script. An alternative option is to add
set -e
...at the start of a script, then any failing command (outside a construct that tests the return code like if ...) will cause the script to stop. It also encourages you to make sure any other scripts that you call will exit with an appropriate return code: 0 for ok, and anything else for failure.
This Tech Tip was brought to us by Mark in France. Thanks, Mark!
Instant fame is easy at Linux Journal. Just send us your useful Tech Tips to share with the Linux Community, and we'll send you a cool t-shirt for your efforts!
Please note: Tech Tips featured in this specific section of LinuxJournal.com are kindly brought to us by readers and are not necessarily tested by LinuxJournal.com editors.
Trending Topics
| You Need A Budget | Feb 10, 2012 |
| The Linux powered LAN Gaming House | Feb 08, 2012 |
| Creating a vDSO: the Colonel's Other Chicken | Feb 06, 2012 |
| Your CMS Is Not Your Web Site | Feb 01, 2012 |
| Casper, the Friendly (and Persistent) Ghost | Jan 31, 2012 |
| Razor-qt 0.4 - Qt based Desktop Environment | Jan 30, 2012 |
- Fun with ethtool
- 100% disappointed with the decision to go all digital.
- Readers' Choice Awards 2011
- Parallel Programming with NVIDIA CUDA
- Validate an E-Mail Address with PHP, the Right Way
- You Need A Budget
- Why Python?
- The Linux powered LAN Gaming House
- Linux-Based X Terminals with XDMCP
- Short Notices: News In Linux Audio
- buena información
3 hours 56 sec ago - One important "bucket" that I didn't note (désolé si qqun deja d
4 hours 1 min ago - Gnome3 is such a POS. No one
13 hours 28 min ago - Gnome 3 is the biggest POS
13 hours 39 min ago - I didn't knew this thing by
19 hours 43 min ago - Author's reply
23 hours 8 min ago - Link to modlys
1 day 14 min ago - I use YNAB because of the
1 day 26 min ago - Search
1 day 5 hours ago - Question
1 day 5 hours ago





Comments
What does "builtin" in the
What does "builtin" in the script do?
Thanks
Another BIG help is 'set
Another BIG help is 'set -u', which treats unset variables as an error. I trashed my system once using rsync: i misspelled a variable name, it expanded to nothing, the trailing / caused it to select the root directory as the destination, and the --delete flag promptly deleted everything on my filesystem. :-(
These are the first non-comment lines in all of my shell scripts now:
set -e
set -u
:-)
Disclaimer
So your disclaimer is for my file cleanup script, isn't it?
"rm -rf /"
Here's the real tip: Don't listen to Shawn. :)
And now for my disclaimer. If you're new to Linux, I was joking. rm -rf is NOT your friend. It will kick your dog, steal your significant other, and call you ugly. Oh, and delete all your files.
Shawn Powers is an Associate Editor for Linux Journal. You might find him chatting on the IRC channel, or Twitter
Slight tweak
If you use "$@" instead of $1, it will work with directory names containing whitespace, as well as handle multiple arguments such as cd -P /path/with/symlink
cd(){
if ! builtin cd "$@"
then echo "Failed to cd $@ !!!" >&2
exit 1
fi
}
Cheers
How is this any better
How is this any better than:
if cd some-place
then
ok
else
fail
fi
Multiple uses
If you script only has one cd in it, it isn't. But, by defining the function at the start of the script you can protect all cd commands within the script.
Phil Hughes
In a correctly written
In a correctly written script, one should be checking the existance and permissions of a directory before trying to change to it - and, if necessary, creating the directory if it doesn't.
Where do these kids learn to program
Thank you.