A small script:
Sometimes I write little scripts partly in order to
play around with a command I haven't used before. Such is the case with
today's script, which I have unimaginatively called
#!/bin/sh # usage: bsearch-nums START END lo=$1; hi=$2 while [ $lo -lt $hi ]; do mid=`echo $lo $hi +2/p | dc` echo "try: $mid" echo -n "u/d? " read a || exit 0 case "$a" in [uU+]*) lo=`echo $mid 1+p | dc`;; [dD-]*) hi=$mid;; esac done echo echo "result: $lo"
Every so often, I find myself running down something where the best way to find the answer is a binary search; today's example was finding out which revision of our aliases file introduced a particular alias. But what usually happens is that only the first few steps are a true binary search and then I resort to jumping around to numbers that look vaguely right, because keeping track of the low and high points and cranking the math gets too annoying.
bsearch-nums, which walks you through the numbers for a manual
binary search. You get to supply the actual testing (and then tell the
program whether the search should go up or down at each step).
As you might guess, the command I was finally trying out was
is one of those hoary old Unix utilities that barely gets any love and
attention these days. (Please don't bother to tell me that a modern
shell could do all the math with builtins. I'm too old fashioned, and
besides, Solaris 8's /bin/sh isn't a modern shell. Although it doesn't
echo -n' either, so I guess this script shows where I actually