Bash Longopts Revision as of Wednesday, 26 June 2024 at 10:57 UTC

#!/bin/bash

script_usage() {
cat <<EOF
USAGE `basename $0` [ OPTIONS ]
   -f1 --flag1
        Sets a parameter
   -f2 --flag2 PARAM
        Provides a parameter
EOF
}

if [ -z "$1" ]; then
    script_usage
else
    while [ -n "$1" ]; do
        case "$1" in
            
            --flag1|-f1)
                echo -e "Flag1 set"
            ;;

            --flag2|-f2)
                shift
                flag2_set=1
                if [ -z "$1" ]; then
                    echo -e "Specify an argument"
                else
                    echo -e "Flag2 specified to be $1"
                fi
            ;;
            
        esac
        shift
    done
fi