Functionalized the guts of the code.
1 parent 7cec515 commit 70651a6151518579b667a8017b7ed62c4f110644
@tundra tundra authored on 7 Oct 2010
Showing 1 changed file
View
206
tpromptuser.sh
#!/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.104 2010/10/07 19:12:17 tundra Exp $
# $Id: tpromptuser.sh,v 1.105 2010/10/07 19:21:14 tundra Exp $
 
# Loop through a set of questions for the user to answer, storing
# their response back into the prompt variable itself.
#
 
ORIGIFS=$IFS # Save current field separator
DL=';' # Prompt field delimiter
 
# ---------- Nothing Should Change Below This Line ---------- #
 
#####
# PromptUser() - Shell Function To Prompt And Validate User Input
#####
 
PromptUser()
 
{
 
for x in $PROMPTLIST
do
eval record=\$$x # Get the prompt record
IFS=$DL # Get individual fields
read prompt default answers <<EOF
$record
EOF
IFS=$ORIGIFS # And restore the original field separator
# Now read input and check it against list of valid responses
DONE=False
while [ _$DONE = _False ]
do
read -p "${prompt} (Default: ${default}) " ANS
# 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" = _ ]
then
DONE=True
# Otherwise, make sure answer is legit
else
for a in $answers # Check against each possible legal answer
do
if [ _$a = _"$ANS" ]
then
DONE=True
fi
done
if [ _$DONE = _False ]
then
echo "Invalid Response! Must Be One Of $answers"
fi
fi
done
# Save the answer back into the original prompt variable,
# substituting the default value if the user entered nothing.
ANS=${ANS:-$default}
eval $x=\"$ANS\"
done
}
# End Of 'PromptUser()'
 
 
 
# ---------- End Of Code ---------- #
 
 
 
 
#####
# Example
#####
 
# Common Answers
 
YN="Y y N n Yes yes No no YES NO"
 
 
#####
# Prompt Records
#####
 
# Layout Of Fields In Prompt Record:
#
# Prompt<delim>Default Response (If user just hits Enter)<delim>Legal Answers (space separated)
 
echo "Before: foo->$foo bar->$bar baz->$baz bat->$bat"
 
 
# ---------- Nothing Should Change Below This Line ---------- #
# Go get the user input and validate it
 
#####
# Actual Prompting Logic
#####
 
 
for x in $PROMPTLIST
do
eval record=\$$x # Get the prompt record
IFS=$DL # Get individual fields
read prompt default answers <<EOF
$record
EOF
 
IFS=$ORIGIFS # And restore the original field separator
 
# Now read input and check it against list of valid responses
DONE=False
 
while [ _$DONE = _False ]
do
 
read -p "${prompt} (Default: ${default}) " ANS
 
# 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" = _ ]
then
DONE=True
 
# Otherwise, make sure answer is legit
 
else
for a in $answers # Check against each possible legal answer
do
if [ _$a = _"$ANS" ]
then
DONE=True
fi
done
 
if [ _$DONE = _False ]
then
echo "Invalid Response! Must Be One Of $answers"
fi
fi
 
done
 
# Save the answer back into the original prompt variable,
# substituting the default value if the user entered nothing.
 
ANS=${ANS:-$default}
eval $x=\"$ANS\"
 
done
 
 
# End Of Prompting Logic
 
PromptUser
 
 
# Dump results for sake of this example only