How to raise and take care of shell scripts: script overclocking
Script tuning
A summary of tuning methods for your scripts. I mentioned the most of them already in the other chapters.
- Avoid unnecessary calls of external programs - try to do as much as possible using shell tools. Every call of an external program creates a new process, this is a resource, which should be used economically - and it costs you processing time. Exception: see below.
- Avoid using loops, if you could do the job using programs, which are designed for it. A awk almost everytime will be faster than your programmed loop - the larger the file the more significant.
- Avoid calling external programs within a loop like the plague! Keep in mind, that this call will create a new process in every round!
- Don't use a sledge-hammer to put a drawing-pin into a cork. I mean: Try to find the right tool to solve your problem. You don't need to use awk to count the characters of a variable - you can do it much cheaper using wc.
- Try to prevent from being "decorated" with the Useless Use Of cat Award ;-) Constructs like
cat file | some_command are almost everytime hot candidates for this award. First, nearly all
Unix / Linux command line tools are able to get file names to process as command line arguments, and if not, they can
be fed using standard input redirection:
some_command <file - Don't use pipes, if they're not necessary. They are often used to process command output - for example to validate
or analyse the output. Prior to build a pipe construct: Look at the command's manual to check if the tool of your choice
provides the same functionality:
if test `grep pattern file | wc -l` -gt 0; then ... - it works, but it's awkward to check if a file content contains a pattern. The grep can do that by himself using the -c option. And suddenly the check looks very much better:
if test `grep -c pattern file` -gt 0; then .... A way to do it even more simplier is described below. - Do you really need the extra test everytime? Keep in mind, that most of the commands exit
with a return value you can evaluate directly. The example I showed above can be written without a test using the
-q option. The option prevents grep from writing any output, the
command returns success if the pattern was found, error if not.This will work faster in many cases because the
grep exits immediately if the pattern was found. Now you can write your pattern check like
this:
if grep -q pattern file; then ...
Better, uhm?

