diff --git a/tpromptuser.sh b/tpromptuser.sh index ca3bb86..df475e4 100755 --- a/tpromptuser.sh +++ b/tpromptuser.sh @@ -1,6 +1,10 @@ #!/bin/sh +# promptuser.sh - User Prompting From Shell With validation +# Copyright (c) 2010, TundraWare Inc, Des Plaines, IL USA +# All Rights Reserved +# $Id: tpromptuser.sh,v 1.103 2010/10/07 18:58:50 tundra Exp $ -# Loop through a set of questions for the luser to answer, storing +# Loop through a set of questions for the user to answer, storing # their response back into the prompt variable itself. # # This is convenient way to build answers to a set of configuration @@ -19,14 +23,20 @@ # single character answers to the prompt. If this list is empty, # then any response to the prompt is accepted. -# $Id: tpromptuser.sh,v 1.102 2010/10/07 18:34:13 tundra Exp $ ##### # Constants ##### +# Separators + ORIGIFS=$IFS # Save current field separator -DL=':' # Prompt field delimeter +DL=';' # Prompt field delimiter + +# Common Answers + +YN="Y y N n Yes yes No no YES NO" + ##### # Prompt Records @@ -34,14 +44,26 @@ # Layout Of Fields In Prompt Record: # -# PromptDefault Response (If user just hits Enter)Legal Answers +# PromptDefault Response (If user just hits Enter)Legal Answers (space separated) +# Notes: +# +# 1) An empty Prompt field means there will be no prompt displayed +# +# 2) An empty Default Response field means that, if the user hits enter, +# the response is blank +# +# 3) An empty Legal Answers field mean the routine will accept *any* input +# +# 4) It's up to you to make sure the Default Response is a legit answer. +# It is NOT checked against the Legal Answers list. + # Examples foo="Would you like to foo?${DL}Y${DL}" # Empty last field means accept anything -bar="Would you like to bar?${DL}\!${DL}y Y n N" -baz="Would you like to baz?${DL}n${DL}yes Yes no No" -bat="Would you like to bat?${DL}${DL}yes Yes no No " +bar="Would you like to bar?${DL}\!${DL}${YN}" +baz="Would you like to baz?${DL}n${DL}${YN} yup" +bat="Would you like to bat?${DL}${DL}nope" # Display the records for purposes of this example only @@ -78,7 +100,7 @@ # We're done if the user took the default OR if the "Legal # Answers" field is empty (which means we accept anything) - if [ _$ANS = _ ] || [ _"$answers" = _ ] + if [ _"$ANS" = _ ] || [ _"$answers" = _ ] then DONE=True @@ -87,7 +109,7 @@ else for a in $answers # Check against each possible legal answer do - if [ _$a = _$ANS ] + if [ _$a = _"$ANS" ] then DONE=True fi @@ -105,8 +127,8 @@ # substituting the default value if the user entered nothing. ANS=${ANS:-$default} - eval $x=\"$ANS\" + done