On 05-Mar-20 6:57, Doug McIlroy wrote:
These go all
the way back to v7 unix, where ls has an option to reverse
the sort order (which
could have been done by passing the output to tac).
A cool idea, but tac was not in v7. And tail didn't get the -r
option until v8.
Tail acquired a -r option between 3BSD [1] and 4BSD [2].
I remember using that option on SunOS in 1990 as part of a prank we
played on a friend at the university. On the Sun 3 workstations we were
using at the time, one could enter the monitor/debugger program by
pressing L1-A. By remotely logging into a workstation and running a
shell loop, one could ensure that when the monitor was entered the
active program would be that shell. It was then easy to modify the uid
field for the active process (the loop-running shell) and set it to
zero. After exiting the monitor, a subshell launched from that shell
would have full root privileges. All we had to do was wait for the
friend to lock his workstation when taking a break in order to obtain
root privileges on his workstation and then change to his uid in order
to modify his files via NFS on the university's Gould file server.
Based on this capability, I wrote the following script that would rename
all our friend's files and directories to words from the dictionary.
The script also created (via tail -r) another script that would undo
this change.
#!/bin/sh
TMP=/tmp
DIR=$1
FILES=$TMP/f.$$
WORDS=$TMP/w.$$
CMD=$TMP/c.$$
REV=$TMP/r.$$
trap '' 0 1 2 3 15
find $DIR -depth -print >$FILES
head -`wc -l <$FILES|sed 's/[ ]*//'` /usr/dict/words >$WORDS
paste $FILES $WORDS |
sed -e '
/^\. /d
s/\(.*\)\/\(.*\) \(.*\)/mv \1\/\2 \1\/\3/
' >$CMD
rm $FILES $WORDS
tail -r $CMD |
sed -e '
s/mv \(.*\) \(.*\)/mv \2 \1/
' >$REV
sh <$CMD
rm $CMD
Unfortunately, it turned out that tail -r had a limit on the number of
lines it could reverse. Although the script and its undo worked fine on
a test set of a small number of files, when run on our friend's
directory it created a faulty undo script. Our friend ended up
graduating with files named "abaca" and "abacinate".
[1]
https://dspinellis.github.io/manview/?src=https%3A%2F%2Fraw.githubuserconte…
[2]
https://dspinellis.github.io/manview/?src=https%3A%2F%2Fraw.githubuserconte…
--
Diomidis Spinellis
Free edX MOOC on Unix Tools: Data, Software, and Production Engineering
https://www.spinellis.gr/unix?tuhs20200306