Search code examples
getopt

Is optstring in getopt case sensitive?


Would the v below parse -V options as well?

getopt -o v

Is it even possible to parse uppercase command options?


Solution

  • answer to your question - getopt is case sensitive , generally it is not recommended to use different cases in script arguments - it can create confusion

    you can think of using multichar inputs in it .

    Try and read about getopt --longoptions.

    Refer below example for the same.

    # Read command line options
    ARGUMENT_LIST=(
        "input1"
        "input2"
        "input3"
    )
    
    
    
    # read arguments
    opts=$(getopt \
        --longoptions "$(printf "%s:," "${ARGUMENT_LIST[@]}")" \
        --name "$(basename "$0")" \
        --options "" \
        -- "$@"
    )
    
    
    echo $opts
    
    eval set --$opts
    
    while true; do
        case "$1" in
        --input1)  
            shift
            empId=$1
            ;;
        --input2)  
            shift
            fromDate=$1
            ;;
        --input3)  
            shift
            toDate=$1
            ;;
          --)
            shift
            break
            ;;
        esac
        shift
    done
    

    and this is how you can call the script

    myscript.sh --input1 "ABC" --input2 "PQR" --input2 "XYZ"
    

    try this , hope this was useful