1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-12-12 09:58:36 +00:00
freebsd/contrib/top/getans
2002-01-24 17:24:16 +00:00

119 lines
2.1 KiB
Bash
Executable File

#!/bin/sh
# getans prompt type default results_filename
# type is one of
# number
# integer
# neginteger
# file default=default filename
# path
# yesno default=0,1 corres yes or no
# string (default)
RAWPMPT=$1
TYP=$2
DFLT=$3
OFNM=$4
ny0="no"; ny1="yes"
if [ ${TYP} = "yesno" ]; then
eval ny=\$ny${DFLT}
pmpt="${RAWPMPT} [$ny]: "
else
if [ -z "${DFLT}" ]; then
pmpt="${RAWPMPT}"
else
pmpt="${RAWPMPT} [${DFLT}]: "
fi
fi
if [ x"`echo -n`" = x-n ]
then
c=\\c
else
n=-n
fi
while :
do
echo $n "$pmpt"$c
read input
case "$TYP" in
number)
tmp=`echo $input | tr -d 0123456789.`
if [ -n "$tmp" ]; then
echo "Invalid number. Please try again."
continue
fi
;;
integer)
tmp=`echo $input | tr -d 0123456789`
if [ -n "$tmp" ]; then
echo "Invalid integer. Please try again."
continue
fi
;;
neginteger)
if [ "x$input" != "x-1" ]; then
tmp=`echo $input | tr -d 0123456789`
if [ -n "$tmp" ]; then
echo "Invalid integer. Please try again."
continue
fi
fi
;;
file)
if [ -z "$input" ]; then
input=${DFLT}
fi
if [ ! -f "$input" -a ! -d "$input" ]; then
echo "The file $input does not exist. Please try again."
continue
fi
;;
path)
if [ -z "$input" ]; then
input="${DFLT}"
fi
if [ ! -f "$input" ]; then
path=`echo $PATH | sed -e s'/::/ . /g' -e 's/:/ /g'`
x=
for elt in $path; do
if [ -f "$elt/$input" ]; then x=1; break; fi
done
if [ -z "$x" ] ;then
echo "The command $input was not found. Please try again."
continue
fi
fi
;;
yesno)
if [ -z "$input" ]; then
input="${DFLT}"
else
case $input in
y | yes)
input=1 ;;
n | no)
input=0 ;;
*)
echo 'Please answer "yes" or "no".'
continue ;;
esac
fi
;;
*) ;;
esac
break
done
if [ -z "$input" ]; then
input="${DFLT}"
fi
echo $input > ${OFNM}