从 man getopt中拷贝出来,简单改了改,更方便看了
#!/bin/bash function mygetopt() { args=`getopt c:n:o:i: $*` if [ $? != 0 ] then echo 'Usage: ...' exit 2 fi set -- $args for i do case "$i" in -c) shift; conf_file=$1; shift;; -i) shift; in_file=$1; shift;; -o) shift; out_file=$1; shift;; -n) shift; number=$1; shift;; --) shift; break;; esac done } function main() { mygetopt $* echo "conf_file: " $conf_file echo "in_file: " $in_file echo "out_file: " $out_file echo "number: " $number } main $*