#!/bin/sh
# Offer to run a complex command from among those listed in specific files:
# ./.frequents and ~/.frequents.
# If there is an argument, it's taken as the line number to choose.
# Otherwise, user is presented with a consolidated list and prompted for
# line number of their choice. In either case the choice is echoed and the
# command line is evalled. (Using eval means the command line can contain
# multiple commands in a pipeline, sequence, and with backgrounded
# components.) Oh, yeah - it skips lines that begin with a "#" hash mark.
# klm August, 2007
unset lines
arg="$1"
read_from () {
if [ -r "$1" ]; then
exec 3< "$1"
while read curline <&3; do
if [ -z "$curline" -o "${curline:0:1}" = "#" ]; then continue; fi
lines[${#lines[@]}]="$curline"
done
exec 3<&-
fi
}
if [ ! .frequents -ef ~/.frequents ]; then
read_from .frequents
fi
read_from ~/.frequents
incr=0
size=${#lines[@]}
if [ "$size" = 0 ]; then
echo "no entries"
exit
fi
if [ -n "$arg" ]; then
which="$arg"
echo -n "$arg "
else
echo
for i in "${lines[@]}"; do
echo $((incr++)): $i
done
echo
echo -n "execute command from line number[default=0, q to cancel]: "
read which
fi
if [ -z "$which" ]; then
which=0
fi
case "$which" in
[0-9] | [0-9][0-9] | [0-9][0-9][0-9]) : ;;
* ) echo cancelled
exit;;
esac
if [ -z "${lines[$which]}" ]; then
echo no match
exit 1
else
echo ":${lines[$which]}:"
eval "${lines[$which]}"
fi