# -------------------------------------------------------------------------- # # Program Information # # -------------------------------------------------------------------------- # COPYRIGHT="__COPYRIGHT__" VERSION=`echo ${CVSID} | cut -d " " -f 3` # -------------------------------------------------------------------------- # # Script Interface Definition # # -------------------------------------------------------------------------- # ##### # Inputs And Command Line Arguments ##### # -h Prints help information # -v Prints program version information ##### # Outputs ##### # - Error output is written to stderr ##### # Side Effects ##### # -------------------------------------------------------------------------- # # Variables, Constants, Literals, And Strings # # -------------------------------------------------------------------------- # ##### # Program Related Stuff ##### HELPTEXT="__FULLNAME__ ${VERSION} - __DESCRIPTION__ \n\ Usage: __FULLNAME__ -XYZh \n\ where, \n\ \n\ -X Option X \n\ -Y Option Y \n\ -Z Option Z \n\ -h Print Help Information \n\ -v Print Version Information \n\ " ##### # Error Messages ##### eBADARGS="Invalid Command Line Argument!" ##### # Stuff Needed By promptuser() Function Below ##### ORIGIFS=$IFS # Save current field separator DL=';' # Prompt field delimiter YN="Y y N n Yes yes No no YES NO" # -------------------------------------------------------------------------- # # Function Definitions # # -------------------------------------------------------------------------- # ##### # Print An Error Message ##### ErrorMsg() { printf "${*}\n" >&2 } # End of 'ErrorMsg()' ##### # Print An Informative Message ##### PrintMsg() { printf "${*}\n" } # End of 'PrintMsg()' ##### # Print Usage Information ##### Usage() { PrintMsg "${HELPTEXT}" } # End of 'Usage()" ##### # Prompt User With Optional Response Validatation ##### # tpromptuser.sh - User Prompting From Shell With validation # Copyright (c) 2010-2012, TundraWare Inc, Des Plaines, IL USA # All Rights Reserved. # For Updates: http://www.tundraware.com/Software/tpromptuser # Id: tpromptuser.sh,v 1.113 2012/06/06 02:26:15 tundra Exp ##### # You are hereby granted a non-exclusive license to do whatever you # like with this software so long as you understand this is # *EXPERMENTAL SOFTWARE* that has not been thoroughly tested. It may # cause harm to your systems, data, and/or network. It may compromise # your security. It may cause other, unspecified, kinds of harm to # your IT environment or business. In other words: # # USE THIS AT YOUR OWN RISK!!!!!! ##### # Takes a list of prompts as arguments tPromptUser() { for x in $* 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 printf "Invalid Response! Must Be One Of: $answers\n" 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 'tPromptUser()' # -------------------------------------------------------------------------- # # Program Entry Point # # -------------------------------------------------------------------------- # ##### # Command Line Processing ##### for cl_argument in ${*} do case ${cl_argument} in -X) PrintMsg "You have selected option: X" ;; -Y) PrintMsg "You have selected option: Y" ;; -Z) PrintMsg "You have selected option: Z" ;; -h) Usage exit ;; -v) PrintMsg ${CVSID} exit ;; *) ErrorMsg ${eBADARGS} Usage exit 1 ;; esac done ###### # Program Entry Point ##### ##### # Example of user prompting ##### # Prompt Records # Layout Of Fields In Prompt Record: # # Prompt<delim>Default Response (If user just hits Enter)<delim>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. # # When the call to tPromptUser returns, the user's response will be stored # *in the variable* that contained the prompting information. # Examples foo="Would you like to foo?${DL}Y${DL}" # Empty last field means accept anything 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" tPromptUser "foo bat" PrintMsg "foo: ${foo}, bat: ${bat}" tPromptUser "bar baz" PrintMsg "bar: ${bar}, baz: ${baz}"