2001-05-22 19:38:22 +04:00
|
|
|
#!/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]: "
|
1999-02-15 02:54:06 +03:00
|
|
|
else
|
2001-05-22 19:38:22 +04:00
|
|
|
if [ -z "${DFLT}" ]; then
|
|
|
|
pmpt="${RAWPMPT}"
|
1999-02-15 02:54:06 +03:00
|
|
|
else
|
2001-05-22 19:38:22 +04:00
|
|
|
pmpt="${RAWPMPT} [${DFLT}]: "
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
if [ x"`echo -n`" = x-n ]
|
|
|
|
then
|
|
|
|
c=\\c
|
1999-02-15 02:54:06 +03:00
|
|
|
else
|
2001-05-22 19:38:22 +04:00
|
|
|
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
|
1999-02-15 02:54:06 +03:00
|
|
|
echo "Invalid number. Please try again."
|
2001-05-22 19:38:22 +04:00
|
|
|
continue
|
|
|
|
fi
|
|
|
|
;;
|
1999-02-15 02:54:06 +03:00
|
|
|
|
2001-05-22 19:38:22 +04:00
|
|
|
integer)
|
|
|
|
tmp=`echo $input | tr -d 0123456789`
|
|
|
|
if [ -n "$tmp" ]; then
|
1999-02-15 02:54:06 +03:00
|
|
|
echo "Invalid integer. Please try again."
|
2001-05-22 19:38:22 +04:00
|
|
|
continue
|
|
|
|
fi
|
|
|
|
;;
|
1999-02-15 02:54:06 +03:00
|
|
|
|
2001-05-22 19:38:22 +04:00
|
|
|
neginteger)
|
|
|
|
if [ "x$input" != "x-1" ]; then
|
|
|
|
tmp=`echo $input | tr -d 0123456789`
|
|
|
|
if [ -n "$tmp" ]; then
|
1999-02-15 02:54:06 +03:00
|
|
|
echo "Invalid integer. Please try again."
|
2001-05-22 19:38:22 +04:00
|
|
|
continue
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
;;
|
1999-02-15 02:54:06 +03:00
|
|
|
|
2001-05-22 19:38:22 +04:00
|
|
|
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
|
|
|
|
;;
|
1999-02-15 02:54:06 +03:00
|
|
|
|
2001-05-22 19:38:22 +04:00
|
|
|
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
|
|
|
|
;;
|
1999-02-15 02:54:06 +03:00
|
|
|
|
2001-05-22 19:38:22 +04:00
|
|
|
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
|
|
|
|
;;
|
1999-02-15 02:54:06 +03:00
|
|
|
|
2001-05-22 19:38:22 +04:00
|
|
|
*) ;;
|
|
|
|
esac
|
|
|
|
break
|
|
|
|
done
|
1999-02-15 02:54:06 +03:00
|
|
|
|
2001-05-22 19:38:22 +04:00
|
|
|
if [ -z "$input" ]; then
|
|
|
|
input="${DFLT}"
|
|
|
|
fi
|
1999-02-15 02:54:06 +03:00
|
|
|
|
2001-05-22 19:38:22 +04:00
|
|
|
echo $input > ${OFNM}
|