Wednesday, December 21, 2011

getopts example in shell script

################################################################
# File          :   getopts.sh
# Author        :   Saurabh Gupta
# Created       :   10:16 AM 12/21/2011
# Description   :   getopts example in shell script
# Source        :   http://www.ccplusplus.com/p/shell-scipt.html
# Note          :   
################################################################

function usage () {
    echo -e "Usage: $0 -e \"EXAMPLE\"  -d \"DESCRIPTION\"  -s \"SOURCE\" "
}

clear

while getopts "e:d:s:" ARG;
    do
    case "${ARG}" in
        e) EXAMPLE="${OPTARG}";;
        d) DESCRIPTION="${OPTARG}";;
        s) SOURCE="${OPTARG}";;
    esac;
done

[ -z "${EXAMPLE}" ] && { usage && exit 1; }

echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
echo "$EXAMPLE by $DESCRIPTION from $SOURCE"
echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"


#
# OUTPUT
#
#XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#getopts example by 'Saurabh Gupta' from http://www.ccplusplus.com/
#XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#

No comments:

Post a Comment