Getopts with optional parameters

Hi Unix Gurus,

i am on learning path of unix, and yet to discover many things. I came across with this requirement where i need to pass parameters but the position of parameters is not fixed so after doing some google search got to know "getopts" can handle that. So here is my code:

function show_help {
    echo "Usage: ${0} -b [bankname] -r [region] -w [windowdate]"
    echo ""
    echo "    -b: BankName"
    echo "    -r: Region"
    echo "    -w: Windowdate"
    exit 0
}
# A POSIX variable

OPTIND=1 # Reset in case getopts has been used previously in the shell.
# set -x

# Get parameters from command line
while getopts "h?b:r:w:f" opt; do
    case "$opt" in
    b)
        BankName=${OPTARG}
        ;;
    r)
        Region=${OPTARG}
        ;;
    w)
        WindowDate=${OPTARG}
	;;
    f)
	FilePattern=${OPTARG}
	;;
	h|\?)
        show_help
	    exit 0
        ;;
    esac
done

shift $((OPTIND-1))
[ "$1" = "--" ] && shift

# If required variables aren't setup, show help and exit
unset MISSING_PARAM
if [[ -z ${BankName} ]]; then
    MISSING_PARAM="${MISSING_PARAM} b"
fi
if [[ -z ${Region} ]]; then
    MISSING_PARAM="${MISSING_PARAM} r"
fi
if [[ -z ${WindowDate} ]]; then
    MISSING_PARAM="${MISSING_PARAM} w"
fi
#if [[ -z ${FilePattern} ]]; then
 #   MISSING_PARAM="${MISSING_PARAM} f"
#fi

if [[ -n ${MISSING_PARAM} ]]; then
    echo "Error: Missing required parameter(s)"
    for PARAM in ${MISSING_PARAM}; do
        echo "    -${PARAM}"
    done
    echo ""
    show_help
    exit 0
fi

well, now my requirement is i want to pass optional parameter, kindly note i am not talkin about optional arguments that i am aware of if i dont use ":" after any option it will accept optional argument for that option, but what i need is optional parameter.. so if i call my scripts like:

./myscript.sh -b ABC -r PQR -w 20180503 -f 130020003_S-VAN-Sort-CSMxx

it does not show any parameter passed to option "-f" because there is no ":" after -f... what i want is out of 4 parameters 3 are mandatory but last one should be non-mandatory so, both should work i.e

./myscript.sh -b ABC -r PQR -w 20180503 -f 130020003_S-VAN-Sort-CSMxx

and

./myscript.sh -b ABC -r PQR -w 20180503

Is there any way i can handle this in one getopts or do i need to write another case for non-mandatory parameter.

Let me know if require any other info.
TIA

You can try this way

# Get parameters from command line
while getopts "b:r:w:f" opt; do
    case "$opt" in
    b)
        BankName=${OPTARG}
        ;;&
    r)
        Region=${OPTARG}
        ;;&
    w)
        WindowDate=${OPTARG}
        ;;&
    f)
        FilePattern="$2"
        ;;&
    b|r|w|f)
        shift 2
        OPTIND=1
        ;;
    *)
        show_help
        exit 0
        ;;
    esac
done
1 Like

I don't think any options are mandatory (that's why they're called what they're called). Mandatory existence needs to be taken care of in the case ... esac construct.

thanks for the suggestion @ctac_
i tried that but it was not giving me desired result as if i give 4 parameters the last parameter i.e. "-f" was taking first parameter's argument so i made some changes and now its working for both cases: here is my final code

function show_help {
    echo "Usage: ${0} -b [bankname] -r [region] -w [windowdate]"
    echo ""
    echo "    -b: BankName"
    echo "    -r: Region"
    echo "    -w: Windowdate"
    exit 0
}
# A POSIX variable

OPTIND=1 # Reset in case getopts has been used previously in the shell.
# set -x

# Default values
TRG=".TRG"


# Get parameters from command line
while getopts "b:r:w:f" opt; do
    case "$opt" in
    b)
        BankName=${OPTARG}
echo ${OPTIND}
        ;;
    r)
        Region=${OPTARG}
echo ${OPTIND}
        ;;
    w)
        WindowDate=${OPTARG}
echo ${OPTIND}
	;;
    f)
	FilePattern=$8  #$2 was giving me RBC so ichanged to $8
echo ${OPTIND}
	;;
    b|r|w|f)
        shift 2
        OPTIND=1
        ;;
    *)
        show_help
        exit 0
        ;;
    esac
done

shift $((OPTIND-1))
[ "$1" = "--" ] && shift

# If required variables aren't setup, show help and exit
unset MISSING_PARAM
if [[ -z ${BankName} ]]; then
    MISSING_PARAM="${MISSING_PARAM} b"
fi
if [[ -z ${Region} ]]; then
    MISSING_PARAM="${MISSING_PARAM} r"
fi
if [[ -z ${WindowDate} ]]; then
    MISSING_PARAM="${MISSING_PARAM} w"
fi

#removed -f missing_param check to ignore it when no argument provided to case -f
if [[ -n ${MISSING_PARAM} ]]; then
    echo "Error: Missing required parameter(s)"
    for PARAM in ${MISSING_PARAM}; do
        echo "    -${PARAM}"
    done
    echo ""
    show_help
    exit 0
fi

so thanks for you reply i got the idea how OPTIND works..let me know if we can tweak it more?