Table of Contents

Shell Script argument parsing (getopts getopt original)



getopts

$ ./test2.sh  -h
Usage: ./test2.sh [-a] [-b dir] item ...

$ ./test2.sh -a -b bbb arg1 arg2
1
bbb
test arg1 arg2
test arg1 arg2

$ ./test2.sh  -b bbb arg1 arg2
bbb
test arg1 arg2
test arg1 arg2
#!/bin/bash

usage_exit() {
        echo "Usage: $0 [-a] [-b dir] item ..." 1>&2
        exit 1
}

while getopts ab:h OPT
do
    case $OPT in
        a)  FLAG_A=1
            #test
            echo $FLAG_A
            ;;
        b)  VALUE_D=$OPTARG
            #test
            echo $VALUE_D
            ;;
        h)  usage_exit
            ;;
    esac
done

shift $((OPTIND - 1))



echo "test $*"
echo "test $@"


getopt

$ ./test3.sh -a aa -d bb -- arg1 arg2

bb
test aa arg1 arg2
test aa arg1 arg2
test aa arg1 arg2
#!/bin/bash

args=`getopt ad: "$@"`
if [ $? != 0 ]; then
    echo "Usage: $0 [-a] [-d dir]" 1>&2
    exit 1
fi
set -- $args
for OPT in "$@"
do
    case $OPT in
        -a) A_FLAG=1
            shift
            #test
            echo $FLAG_A
            ;;
        -d) B_ARG=$2
            shift 2
            #test
            echo $B_ARG
            ;;
        --) shift
            echo "test $*"
            break
            ;;
    esac
done


echo "test $*"
echo "test $@"


Original Parceing

$ ./arg_test.sh -a aaaa -b bbbb -c -- arg1 arg2
test-a  aaaa
test-b bbbb
test-c
test arg1 arg2

$ ./arg_test.sh -a aaaa -b bbbb -c arg1 arg2
test-a  aaaa
test-b bbbb
test-c
test*  arg1
test*  arg1
test
#!/bin/bash
#arg_test.sh
#
#./arg_test.sh -a aaaa -b bbbb -c arg1 arg2
#./arg_test.sh -a aaaa -b  -c arg1 arg2
#
#
PROGNAME=$(basename $0)
VERSION="1.0"

usage() {
    echo "Usage: $PROGNAME [OPTIONS] FILE"
    echo "  This script is ~."
    echo
    echo "Options:"
    echo "  -h, --help"
    echo "      --version"
    echo "  -a, --long-a ARG"
    echo "  -b, --long-b [ARG]"
    echo "  -c, --long-c"
    echo
    exit 1
}

while [ "$#" -gt 0 ]
do
    echo 'Debug $1' " = $1 : ALL ARG =  $@"
    case $1 in
        -h | --help)
            usage
            exit 1
            ;;
        --version)
            echo $VERSION
            exit 1
            ;;
        -a | --long-a)
            if [[ -z "$2" ]] || [[ "$2" =~ ^-+ ]]; then
                echo "$PROGNAME: option requires an argument -- $1" 1>&2
                exit 1
            fi
            ARG_A=$2
            shift 2
            #test
            echo "test-a  $ARG_A"
            ;;
        -b | --long-b)
            if [[ -z "$2" ]] || [[ "$2" =~ ^-+ ]]; then

                shift
            else
                #test
                echo "test-b $2"

                shift 2
            fi
            ;;
        -c | --long-c)
            shift 1

            #test
            echo "test-c "
            ;;
        -- | -)
            shift 1
            param+=( "$@" )
            break
            ;;
        -*)
            echo "$PROGNAME: illegal option -- '$(echo $1 | sed 's/^-*//')'" 1>&2
            exit 1
            ;;
        *)
            if [[ ! -z "$1" ]] && [[ ! "$1" =~ ^-+ ]]; then
                #param=( ${param[@]} "$1" )
                param+=( "$1" )
                shift 1
                #test
                echo "test*  $param"
            fi
            ;;
    esac
done

#test
echo "test $*"

if [ -z "$param" ]; then
    echo "$PROGNAME: too few arguments" 1>&2
    echo "Try '$PROGNAME --help' for more information." 1>&2
    exit 1
fi


Refarence