Wednesday, September 15, 2010

Processing input arguments in bash shell scripting

 
# Function to print the usage of this script
Usage ()
{
    cat <<EOF
Usage: `basename $0` options [-kld]
          -k <val_k>   Value for the the option -k
          -l <val_l>   Value for the the option -l
          -d           Does not have any value
          -e           Does not have any value
          -h           Print this message
EOF
exit $E_OPTERROR
}
 
# Check for input arguments. If no arguments given, print the usage.
if [ $# -eq 0 ]; then
    Usage
fi
 
while getopts ":k:l:de" Option
do
    case $Option in
        k ) echo "value for the option [k] is \"$OPTARG\""
            ;;
        l ) echo "value for the option [l] is \"$OPTARG\""
            ;;
        d ) echo "option [d] is given."
            ;;
        e ) echo "option [e] is given."
            ;;
        h ) Usage
            ;;
        * ) echo "Invalid input option"
            Usage
            ;;
    esac
done

# To get the arguments other than the option given in the usage
shift $(($OPTIND - 1))
 
for i in $*
do
    echo "Extra inputs given -> \"$i\""
done

exit 0

No comments :

Post a Comment