Unix: Writing more efficient shell scripts

Want to tune your shell scripts to run more efficiently? Let's check out a few changes that might help them run faster.

By  

The most important thing to think about when you're preparing scripts is whether they work properly -- are doing what they're meant to do and not causing any problems that you didn't expect. Scripts should be reliable, should fail gracefully if the user calls them with the wrong arguments and do only predictable things. At the same time, some scripting techniques allow scripts to run much more efficiently. So let's take a look at some techniques that might get your scripts running faster.

One of the first things you can do to make your scripts run more efficiently is to reduce the amount of output that your script sends to the screen. In general, it's a good idea for a script to produce only as much output as is required for the script to do the work intended and for the person running the script to be confident that it's working. Sending output to the screen takes a lot of time compared with other commands that you are likely to use in your scripts. So, if the point of your script isn't to send a lot of output to the screen, don't.

The echo command in this script might show the user that your script is running through the days of the week -- in this case to create some logs, but lots of extraneous echo commands can slow a script siginificantly, especially if it's handing a tremendous amount of data.

for day in Mon Tue Wed Thu Fri
do
    echo $day
    touch $day.log
done

One way to reduce unnecessary output is to provide a verbose option so that anyone using your scripts can see additional output when -- and only when -- they need to better understand how your scripts are working or to troubleshoot some problem they are having. Most of the time, however, that output will not be needed and could be suppressed.

for day in Mon Tue Wed Thu Fri
do
    if [ $verbose ]; then echo $day; fi
    touch $day.log
done

You should also, whenever possible, limit how much you write to external files. Interaction with the file system is another time-consuming operation. Instead of using temporary files to hold information that a script requires, you might, instead, store data in data structures inside your scripts. Without involving the file system, your scripts will likely run significantly faster.

Depending on the complexity of your scripts, the difference in timing might be trivial or extremely significant. I have sat and watched scripts that seemed to run on and on when, when output to the screen or interaction with external files was removed, finished within seconds.

Another time-saving option revolves around the logic that you use in your scripts.
When possible, prioritize your if tests so that you are likely to run as few tests as possible. This is especially true if you are running through a long series of tests in an if then else structure.

Photo Credit: 

fotolia

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Operating SystemsWhite Papers & Webcasts

See more White Papers | Webcasts

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Ask a Question