Our computing machines are endowed with myriad goodly powers, but in the Beginning they were made for one thing: to loop. O, and also to count. Two things, then: to loop and to count. And to process, of course. These three things, then, they were made for: to loop, to count, and to process. And to be programmable, all right, that as well. Amongst the things they were made for, are such diverse elements as . . . I’ll come in again.

Verily they have their loops in High Level Languages, in their Rubie or Pythonne or JavaScripte or C-Flat. But this profoundly pow’rful construct is also readily available on the Unicks line of command, whereupon it is wondrous useful. There are sundry ways to achieve said looping, first at hand today is the shell built-in ‘for’, eff oh arr.

A Sawfish

The incantation spans multiple Lines and has some subtleties and wiles, but it is not over complicated. The first Line contains the keyword itself, then a variable Name of thy choosing, then the keyword, ‘in’, and finally the list of Items to be subsequently regarded, each in turn. Then the ‘enter’ key, at which point ye shall behold the secondary shell prompt, the right-facing battle pennant: ‘>’. Type then the single keyword, ‘do’, and press ‘enter’ once more.

Now thou art ready to enter the commands which will act upon each of your Items. It is not required to indent these commands, though I feel it proper. End each command by pressing ‘enter’ again. When thy commands are concluded, add one last Line with the final keyword ‘done’. Id est:

ℜ> for c in Second Third Fourth > do > echo "$c Crusade" >> knights_ii_iii_iv.txt > cat Crusades/$c/knights.txt >> knights_ii_iii_iv.txt > done ℜ> _

Leaving aside the ‘echo’ command in this Example, one might wonder if this may have been accomplished instead with a single, simple ‘cat’ command and clever use of wyldcards. Alack, the order of the Directory names would have been governed by the divine principle of Alphabeticality — Fourth, then Second, then Third — which was not desired in this case.

An Ibis with Two Fish in its Beak

It is less common to thus list static Items for ‘for’ to iterate, and rather more common to employ the Globbing of Filenames. As before, the advantage of ‘for’ over one-liners with wyldcards is that multiple statements may be issued therein. Id est:

ℜ> ls M* MER_Guntram MER_Sigebert_III MER_Dagobert_II ℜ> for olde in MER_* > do > # back-ticks to execute the commands > new=`echo $olde | sed 's/MER_/Merovingian_/'` > mv $olde $new > done ℜ> ls M* Merovingian_Guntram Merovingian_Sigebert_III Merovingian_Dagobert_II ℜ> _

The back-tick execution in that Example brings us to a third and wholly wonderful way to use this looping device. For that execution may itself be used to generate the list of Items that ‘for’ will dutifully peruse, one by one. The possibilities are as limitless as the herds of Unicorns which crowd the countryside and ravage our crops; here is but one advantageous example:

ℜ> cat orders.txt Knights Hospitaller Order of Calatrava Knights Templar Order of Aviz Order of Aubrac Order of Santiago Teutonic Knights Order of Mountjoy ℜ> for k in `grep Knights orders.txt | sed 's/ /_/g'` > do > mkdir $k > done ℜ> ls -d *Knights* Knights_Hospitaller Knights_Templar Teutonic_Knights ℜ> _

As shewn in these Examples, the list of Items provided for ‘for’ to iterate o’er may be divided by any white-space, be it space, or tabbe, or newline.

Ctrl-D, Amen.

A Sawfish, after 1277, Franco-Flemish
An Ibis with Two Fish in its Beak, after 1277, Franco-Flemish
Digital images courtesy of the Getty’s Open Content Program

~ this page pub. 01 December 2013 ~
« previouse page: Wyld Kingdom: beavers ~ Table of Contentes ~ next page: Sponsors »