Sneaky performance problems in shell scripts

On many systems, the backtick or $() operator causes a temporary file to be used to capture the output of the command before shoving it into the variable.  Often times these are by design only intending to capture and process one line (or even one word!) of output.

Many of these uses can be refactored into reading from a pipeline:

NEW_PATH=`foo|grep bar | ...`
 ... $NEW_PATH ...
 ... $NEW_PATH ...
 ... $NEW_PATH ...

becomes

foo|grep bar | while read NEW_PATH; do
   ... $NEW_PATH ...
   ... $NEW_PATH ...
   ... $NEW_PATH ...
done

On a degenerate, but not at all staged shell script that I looked at, this cut the execution time by 3x (which was important because it took in excess of 90 seconds to run)

Comments

Popular posts from this blog

systemd unit for WebSphere Liberty (wlp)

Showing HTTP requests with no HTTP responses in wireshark

openssl and atexit