Saturday, March 12, 2011

[ekjksrpq] Shell script for binary search

Uses the return value of the program $1 to determine success.

#!/bin/bash
set -x
if [ -z "$1" ]
then echo "need program"
exit 1
fi
lo=1
hi=1000000000
while expr \( $lo + 1 \) \< $hi
do mid=`expr \( $lo + $hi \) / 2 `
if time "$1" $mid
then
lo=$mid
else
hi=$mid
fi
sleep 1
done

No comments :