Wandering Thoughts archives

2008-10-21

Seeing how remarkable V7 Unix was

One of the interesting things that the Unix Heritage Society does is that it lets us go back and see just what a remarkable thing Seventh Edition Unix was, how much it was an aesthetic flowering and really created a lot of what we now think of as 'Unix'.

It does this not so much by letting us going back to see V7 as it was (by modern standards, V7 is nothing terribly impressive and is in some ways rather primitive). Rather, it does this by letting us go back and see the Sixth Edition (V6) as well as V7. Looking at both versions makes it very clear just how much of a jump there was between V6 and V7, and thus how much of what we think of as 'the Unix way' only appeared in V7 and was not a straightforward evolution from V6.

My impression is that this flowering was not so much from the low level details of the system (the V6 and V7 system call lists are pretty similar, for example), but from higher level programs and decisions. Many of what we think of as signature Unix programs only appeared in V7, things like awk, make, even sed. (And the Bourne shell.)

Sidebar: the V6 shell

The V6 shell is (or seems to be) by modern standards a rather peculiar beast. I particularly enjoy how commands like exit and its looping construct were implemented: rather than being builtins, they used seek() to change the position of the shell's command file descriptor, so that the shell would read a different next command. (Or in the case of exit, so that the shell would immediately see end-of-file.)

RemarkableV7 written at 00:28:00; Add Comment

2008-10-11

Forcing sort ordering in Unix shell scripts

There are a number of situations in Unix shell scripts where you know the order that the lines in your output should be in, but you can't produce it in that order and nothing in your normal output is useful to sort on. One common case is awk scripts that accumulate information into arrays and then dump it out in their END blocks with 'for (key in array) ...'; array keys are, of course, produced in no particular order.

In this situation, some people go to bat against sort with complex key specifications and the like. I'm lazy, so my usual solution is simple: I add an extra field at the start of the line that has a useful key for sort, sort the output, and then run the output through a second awk script to remove the first field (and often to do the final neat formatting of the output).

A variant of this trick can be used to re-order output lines that are easiest to produce together. A snippet from one of my shell script is perhaps the best illustration:

.... |
(while read fs typ opts; do
     echo 0 unshare $fs
     echo 2 share -F $typ -o $opts $fs
 done;
 echo 1 sleep 60) |
sort -n | sed 's/^[012] //' | ...

What this script wants to do is unshare a bunch of filesystems, wait some time, and then reshare them all. However, it gets all of the necessary information about each filesystem once, in a big chunk, so it is most natural to generate both the unshare and the reshare command at the same time. To insert a sleep in the middle we add a hidden field and just pick entirely artificial keys for all the lines such that sort will put all of the unshares first, the sleep second, and then all of the shares third.

(Disclaimer: this is clearly at least related to Perl's Schwartzian transforms, although I think that they're not quite the same thing, and I probably picked up this shell idiom from somewhere.)

ForcingSortOrder written at 02:03:39; Add Comment


Page tools: See As Normal.
Search:
Login: Password:
Atom Syndication: Recent Pages, Recent Comments.

This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.