Command line trick: track progress with `set -`
tl;dr: "$@" can be assigned to a list of items to process manually,
so $1 is the next one to process, and `shift` is invoked when we are
done with it.
In POSIX shell, variable types are pretty much restricted to scalar
strings. It is of course possible to set a variable with the
concatenation of ${IFS}-separated strings, but it is usually a poor
replacement of a proper array.
Unless relying on some interpreter that does implement arrays as an
extension (e.g. bash), the only 'real' array is the "$@", the argument
vector. Items can be accessed as $1, $2, ...etc, it can be shifted
with the `shift` operator. I think not everybody knows it is possible
to assign it like this:
$ set - arg1 arg2 ...
I believe it is not common to assign "$@". For sure I never seen it
done in the wild, nor I felt the urge of doing it myself. This until
a few days ago, when I was moving all phlog entries from my old phlog
(
gopher://tilde.institute/1/~dacav) to here.
The migration was accomplished by processing each entry with a little
conversion script that I wrote for the purpose. Given the small number
of entries I had to process, I decided to keep it simple, and make
up for its lack of generality by manually verifying and fixing each
converted entry.
Basically, I had to manually loop over entries, but I was easily
distracted by the manual verification I was doing at each step.
That's when I figured that I could an array to track my progress: the
"$@" of the interactive interpreter!
In short, my workflow became the following:
$ set - ./to-import/*
$ more $1 # view and decide if the entry should be kept
$ ./process-entry $1 # if we want to keep it, process it. Then
$ # verify the result and possibly fix it.
$ shift || echo done. # proceed to the next entry...
$ more $1 # view and decide if the entry should be kept
$ ./process-entry $1
This is a quite generic trick, and I started using it each time I've
got a collection of objects to handle.