With my new book “Oracle Shell Scripting” now on shelves you may be wondering what shell scripting can do for you. Below is an excerpt from the book that touches on when you may want to think about scripting. While the book is geared toward Oracle users much of the content would apply to shell scripting regardless of the use.
When to script
A shell script can be written to do anything you would do at the command line. So when do you want to write a shell script? When is it a bad idea? Well, here are a few guidelines I use.
Shell scripts can yield the biggest return on regular tasks that are performed more-or-less the same way each time. If every day you log into a system and remove some files out of a directory, backup a database or check a log file for a specific string of characters you should automate these tasks. Chances are you can make them run automatically and save yourself the hassle all together.
Shell scripts aren’t just for automation though. In some cases a script saves us time in a different way by allowing us to run a simple command instead of a very complicated one.
One of the most powerful features of the shell and shell scripting is the ability to affect several files and even multiple servers with a single script. Loops and commands like find make shell scripting ideal for managing large numbers of files in a single step.
In general there is little point in writing a shell script to do something once. The exception to this is occasionally something needs to be run at a time when you would rather not have to be around to run it yourself. In this case you may decide to bundle those commands into a shell script and schedule it to run without you involved. Be careful with this type of script and always be thinking “what if something goes wrong?” because sooner or later it will and if you’re not around to fix it you could get in some hot water.

Buy it now!
“In general there is little point in writing a shell script to do something once. ”
I’d have to disagree on this … quite often you may find yourself needing to do something only once, or once in a very great while, but a script is justified when it saves time. The time of a sysadmin or a dba is very valuable, even if you only save 10 minutes by writing a script, that’s still worth it in most cases. Scripts are also very good at repetitive tasks, something humans aren’t very good at, and are likely to screw up; the script on the other hand is much less likely to make a mistake.
A good point Al. Spending 15 minutes to write a script that will save you hours of work is definitely worth it, but often, especially for scripting novices, it may be easier to just perform the task manually.
I’d still stand by this generalization, but yes, there are countless examples where a one-time script can save you time, mistakes and frustration.
I generally do not write much shell scripts these days. During the 10 years that I have been using Perl I have found it very useful for almost all of the command line work I need to do. I still write shell scripts when I need to do something that will take a long time but require relatively few commands. e.g Decompressing a bunch of very large logs files that are individual piped to a perl script for processing for say end of year reporting.