O’Reilly howto’s for noobs in BSD

Posted in BSD

I just thought I found a really bad flaw in an O’Reilly book that I starting thumbing through entitled “BSD Hacks - 100 Industrial Strength Tips & Tools” written by Dru Lavigne. In one of the very hacks the author enlists the reader to create his or her very first script. “If you’ve never written a script before, this is an excellent exercise in how easy and useful scripting can be.” I laughed after reading this because it’s also an exercise in how much damage such a small bug unintended feature can do.

The hack is called “Create a Trash Directory” and it works just fine if you’re deleting just one item, or multiple items using just one argument. You delete something, and a short little script that you have aliased in place of “rm” actually just moves whatever you intended to delete into a directory somewhere under your home directory, most likely named “.trash”. But the simple three line script only took into account a single command line argument, dollar one. If you had a directory with files “junk.sh”, “trash.txt”, “dontdelete.me”, and “important.doc”. And you typed:

$ rm *

then ok, fine. You just moved everything in `pwd` into your trash directory and you can recover it later. But, if you typed:

$ rm junk.sh trash.txt important.doc

to delete just a subset of files, then you’re up a creek without a paddle when you decide you really want those last two files back. The script implemented in the book only the first argument “junk.sh” into account. trash.txt and important.doc were $2 and $3, but your script didn’t handle those arguments. Only the regex covered by the dollar one argument is now recoverable. Ooops!

What you really wanted to do with the trash script is delete all the parameters using $@. The hack never really explains this for the command line handicapped! The hack does cover this situation in the end, asking if you spotted the flaw, but what if you were testing your script as you wrote it instead of reading the whole thing? So like I said, I thought I found a flaw, but I didn’t! It would have been nice for the command line noob if they explained what why and how!!!

Posted by admica   @   15 February 2009

Related Posts

Like this post? Share it!

Digg Twitter StumbleUpon Delicious Technorati Facebook RSS

0 Comments

No comments yet. Be the first to leave a comment !
Leave a Comment

Name

Email

Website

Previous Post
« No ssh port forwarding for you!
Next Post
Convert PDF’s to JPG image files »
Powered by Wordpress   |   Lunated designed by ZenVerse