Shell while Loop Considered Harmful
Whoops, silent data loss. POSIX requires that text files end in an ultimate newline to be considered text files, but in practice that ultimate newline may be absent and then...yeah. How many shell scripts are out there and how many ultimate lines have been lost this way?
A less terrible version is, well, verbose.
And, slow. Do you want under a second (C, Perl), or seven seconds (ksh)?
Typically the shell will be a order, or orders, of magnitude slower, especially when it forks external tools; the above uses the shell internal echo to make the numbers for the shell less bad. echo is not portable nor safe for random input, but the portable printf(1) involves a fork which would make the shell performance even worse.
Okay for fast prototyping, terrible for most anything else.
Therefore, if shell code will have something tricky and non-performant like a while loop in it, I generally write that code in some other language.
The Benchmark Code
Pattern Recognition
Commands or groups of commands that are often run should probably be rewritten to be more efficient; consider counting the most frequent of input lines, which might realistically be some portion of a logfile:
This gets the job done, but is slow. Somewhat faster is to place all the lines into a hash of line => count pairs, and then to sort by the count, all within a single process. More efficient, but you have to actually notice the pattern, worry about the CPU waste, and then write a specific tool for it.
https://thrig.me/src/scripts.git
tags #ksh #c #sh #perl #lisp