Listing 11
#
# Ask user to select from list of files in given text file
# usage: lpick.sh "descrip" <file>
#
[ $# -ne 2 ] && echo usage: "$0 \"descrip\" <list-file>" && exit 1
[ ! -r $2 ] && echo "$0: no file named $2 found." && exit 1
flist=$2
if [ ! -s $flist ]
then
echo "There are currently no $1 to pick from.">&2
echo "ABORT"
exit
fi
echo >&2
echo "The following $1 are present:">&2
echo >&2
awk '{printf("%2d) %s\n", count+1, $1); count = count + 1}' <$flist >&2
nfiles=`expr \`cat $flist | wc -l\`` # number of lines to pick from
while true
do
echo>&2
echo "Please select one BY NUMBER (Return alone to abort): \c">&2
read n
if [ "$n" = "" ]
then
echo "ABORT"
exit
fi
if [ $n -lt 1 -o $n -gt $nfiles ]
then
echo "Sorry, that number is out of range.">&2
else
n=`expr $n - 1` # adjust for use in upcoming awk script
break
fi
done
stub=`awk '{ if (count == '$n') print $1; count = count + 1 }' <$flist`
echo $stub
|