27.08.2015 Views

Advanced Bash−Scripting Guide

Advanced Bash-Scripting Guide - Nicku.org

Advanced Bash-Scripting Guide - Nicku.org

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

# 2) Add options for colored backgrounds.<br />

<strong>Advanced</strong> <strong>Bash−Scripting</strong> <strong>Guide</strong><br />

There is, however, a major problem with all this. ANSI escape sequences are emphatically<br />

non−portable. What works fine on some terminal emulators (or the console) may work differently, or<br />

not at all, on others. A "colorized" script that looks stunning on the script author's machine may produce<br />

unreadable output on someone else's. This greatly compromises the usefulness of "colorizing" scripts,<br />

and possibly relegates this technique to the status of a gimmick or even a "toy".<br />

Moshe Jacobson's color utility (http://runslinux.net/projects/color) considerably simplifies using ANSI escape<br />

sequences. It substitutes a clean and logical syntax for the clumsy constructs just discussed.<br />

34.6. Optimizations<br />

Most shell scripts are quick 'n dirty solutions to non−complex problems. As such, optimizing them for speed<br />

is not much of an issue. Consider the case, though, where a script carries out an important task, does it well,<br />

but runs too slowly. Rewriting it in a compiled language may not be a palatable option. The simplest fix<br />

would be to rewrite the parts of the script that slow it down. Is it possible to apply principles of code<br />

optimization even to a lowly shell script?<br />

Check the loops in the script. Time consumed by repetitive operations adds up quickly. If at all possible,<br />

remove time−consuming operations from within loops.<br />

Use builtin commands in preference to system commands. Builtins execute faster and usually do not launch a<br />

subshell when invoked.<br />

Avoid unnecessary commands, particularly in a pipe.<br />

cat "$file" | grep "$word"<br />

grep "$word" "$file"<br />

# The above command lines have an identical effect,<br />

#+ but the second runs faster since it launches one fewer subprocess.<br />

The cat command seems especially prone to overuse in scripts.<br />

Use the time and times tools to profile computation−intensive commands. Consider rewriting time−critical<br />

code sections in C, or even in assembler.<br />

Try to minimize file I/O. Bash is not particularly efficient at handling files, so consider using more<br />

appropriate tools for this within the script, such as awk or Perl.<br />

Write your scripts in a structured, coherent form, so they can be reorganized and tightened up as necessary.<br />

Some of the optimization techniques applicable to high−level languages may work for scripts, but others, such<br />

as loop unrolling, are mostly irrelevant. Above all, use common sense.<br />

For an excellent demonstration of how optimization can drastically reduce the execution time of a script, see<br />

Example 12−32.<br />

Chapter 34. Miscellany 346

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!