コマンドラインオプションを解析する

#!/bin/sh
#getopts.sh

opt_f="(none)"
opt_g="(none)"
opt_h="NO"

while getopts f:g:h flag; do
    case $flag in
        f)
            opt_f="$OPTARG";;
        g)
            opt_g="$OPTARG";;
        h)
            opt_h=YES;;
    esac
done

echo "-f $opt_f"
echo "-g $opt_g"
echo "-h $opt_h"

shift `expr $OPTIND - 1`
echo "other args: $*"
$ ./getopts.sh -f opt1 -h arg1 arg2
-f opt1
-g (none)
-h YES
other args: arg1 arg2