シェルスクリプトで引数処理をしてくれるgetoptが
どうも上手く使えなかったので整理してまとめ。
既に似た情報は山程ネットにあふれているけど
どうも一目でわからなかったので書き直し。
とりあえず使える用なので
他のオプションとかgetoptsとの区別とかは無視。
#!/bin/sh ######################################## GETOPT_ARG_SHORT="acd:e:" GETOPT_ARG_LONG="one,two,three:,four:" ######################################## getopt_template(){ OPT=`getopt -o ${GETOPT_ARG_SHORT} -l ${GETOPT_ARG_LONG} -- $*` echo "opt=${OPT}" while [ -n "$1" ];do case $1 in -a) opt_a=1;shift;; -b) opt_b=1;shift;; -c) opt_c=$2;shift 2;; -d) opt_d=$2;shift 2;; --one) opt_one=1;shift;; --two) opt_two=1;shift;; --three) opt_three=$2;shift 2;; --four) opt_four=$2;shift 2;; --) shift;break;; *) echo "error";return 1;; esac done return 0 } getopt_template $@ status=$? if [ ${status} -ne 1 ] ;then echo "a="${opt_a} echo "b="${opt_b} echo "c="${opt_c} echo "d="${opt_d} echo "one="${opt_one} echo "two="${opt_two} echo "three="${opt_three} echo "four="${opt_four} fi