Newer
Older
tmkproject / sh / program.sh
@tundra tundra on 1 Jun 2012 4 KB Initial revision
#####
# Program Information
#####

COPYRIGHT="__COPYRIGHT__"
PROGNAME="__PROJECTNAME__"

#####
# 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="${CVSID}                                              \n\
             Usage:  ${PROGNAME} -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"


# 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.


# 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"


PROMPTLIST="foo bar baz bat"


#
# Common Program Locations
#

AWK="/usr/bin/awk"
ECHO="/usr/bin/echo"
LS="/usr/bin/ls"
PERL="/usr/bin/perl"
PRINTF="/usr/bin/printf"
PYTHON="/usr/bin/python"
SED="/usr/bin/sed"


#####
# Function Definitions
#####

#
# Print An Error Message
#

ErrorMsg()
{
  ${PRINTF} "${*}\n" >&2
}

# End of 'ErrorMsg()'


#
# Print An Informative Message
#

PrintMsg()
{
  ${PRINTF} "${*}\n"
}

# End of 'PrintMsg()'


#
# PromptUser() - Shell Function To Prompt And Validate User Input
#

# From: promptuser.sh,v 1.104 2010/10/07 19:27:37 tdaneli Exp

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()'


#
# Print Usage Information
#

Usage()
{
  PrintMsg "${HELPTEXT}"
}

# End of 'Usage()"


#####
# Command Line Processing
#####

for cl_argument in ${*}
do

  case ${cl_argument}
  in

    -h)
      Usage
      exit
    ;;

    -v)
      PrintMsg ${CVSID}
    ;;

    *)
      ErrorMsg ${eBADARGS}
      Usage
      exit 1
    ;;

  esac

done


######
# Program Entry Point
#####