#! /bin/sh #---------------------------------------------------------------------------- # /var/install/bin/ask - ask user # # Copyright (c) 2003 Frank Meyer # # Creation: 19.07.2003 fm # Last Update: $Id$ # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. #---------------------------------------------------------------------------- #---------------------------------------------------------------------------- # # Syntax: # # /var/install/bin/ask QUESTION [DEFAULT] [PATTERN] [PATTERN] ... # # QUESTION - Question to be asked. # DEFAULT - Default answer # PATTERN - All possible Answers: # empty - y(es) or n(o) # * - string input # + - string input (not empty!) # min-max - numerical input (range given) # WORD|WORD - a list of possible values # the first will be included into the question # the last will be returned if choosen # # Retur Values: # # Exitcode: Zero-Based Index of the Choosen PATTERN # 255 ask was aborted by typing CTRL-C # Output: If Output is redirected, the output is the last part # of the matching pattern (if PATTERN is given) or the # entered text # # Examples: # ask "Kernel löschen" # ask "Kernel löschen" "no" # ask "Cronzyklus" "" "d|daily" "w|weekly" "m|monthly" # ask "Hostname" "eistest" "*" # ask "Hostname" "" "+" >result # ask "Weiter" >result # ask "Anzahl" "1" "1-7" >result # ask "Anzahl" "" "1-15" >result # #---------------------------------------------------------------------------- trap 'echo >/dev/tty;exit 255' SIGINT question="$1"; shift default="$1"; shift ### parameter processing if [ $# -eq 0 ] # use yes/no as default then set -- "y|yes" "n|no" values="y/n" full_values=" y(es) or n(o)" elif [ $# -eq 1 -a "$1" = "*" ] # string (empty allowed) then set -- ".*" values="" full_values="" elif [ $# -eq 1 -a "$1" = "+" ] # string (not empty) then set -- ".+" values="" full_values="" elif [ $# -eq 1 -a "`echo "$1" | grep -E '[^0-9\-]' 2>/dev/null`" = "" ] then from=`echo "$1" | sed s/-.*$//` to=`echo "$1" | sed s/^.*-//` set -- "[0-9]+" values="" full_values=" ($from-$to)" else # list of values ### Build strings of possible values for output values="" full_values=" " idx=1 while [ $idx -le $# ] do eval regexp='$'$idx full_values="$full_values$regexp" regexp=`echo "$regexp" | sed -e "s/|.*//"` values="$values$regexp" if [ $idx -lt $# ] then values="$values/" full_values="$full_values or " fi idx=`/usr/bin/expr $idx + 1` done fi ### build question if [ -n "$values" ] ; then question="$question ($values)" ; fi if [ -n "$default" ] ; then question="$question [$default]" ; fi question="$question? \c" while [ 1 ] do ### ask the question echo -e "$question" >/dev/tty ### read the answer read a a=${a:-$default} ### is the answer allowed? idx=1 while [ $idx -le $# ] do eval regexp='$'$idx echo $a | grep -qE "^($regexp)$" if [ $? -eq 0 ] then if [ "$from" = "" ] || [ "$from" -le "$a" -a "$to" -ge "$a" ] then if ! /usr/bin/tty -s <&1 then if [ "$1" = ".*" ] || [ "$1" = ".+" ] || [ "$from" != "" ] then echo "$a" else echo "$regexp" | sed -e "s/.*|//" fi fi exit `/usr/bin/expr $idx - 1` fi fi idx=`/usr/bin/expr $idx + 1` done colecho "Please answer$full_values!" br x br >/dev/tty done exit 255 # not reached