diff --git a/tpromptuser.sh b/tpromptuser.sh new file mode 100755 index 0000000..0cefd89 --- /dev/null +++ b/tpromptuser.sh @@ -0,0 +1,115 @@ +#!/bin/sh + +# Loop through a set of questions for the luser to answer, storing +# their response back into the prompt variable itself. +# +# This is convenient way to build answers to a set of configuration +# questions at the beginning of a script. +# +# This is cleaner than having an explicit prompt stanza for each +# variable in the script that needs to be set. +# +# Note that this uses the "eval ...\$$" rather than the more modern +# ${!var} idiom for indirect variable references. This is for +# portability. The latter form is a bash-ism and is not supported in +# a traditional Bourne Shell. +# +# This version implements a prompt table scheme that allows the +# specification of a default response and a list of legitimate +# single character answers to the prompt. If this list is empty, +# then any response to the prompt is accepted. + +# $Id: tpromptuser.sh,v 1.101 2010/10/07 18:26:58 tundra Exp $ + +##### +# Constants +##### + +ORIGIFS=$IFS # Save current field separator +DL=':' # Prompt field delimeter + +##### +# Prompt Records +##### + +# Layout Of Fields In Prompt Record: +# +# PromptDefault Response (If user just hits Enter)Legal Answers + +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 " + + +# Display the records for purposes of this example only + +echo "Before: foo->$foo bar->$bar baz->$baz bat->$bat" + +##### +# Actual Prompting Logic +##### + + +for x in "foo" "bar" "baz" "bat" +do + eval record=\$$x # Get the prompt record + IFS=$DL # Get individual fields + read prompt default answers <$foo bar->$bar baz->$baz bat->$bat" +