shell之getopt的用法

1,在编写带有多参数的shell脚本时,需要用到getopt,语法比较多,先留个例子,以备后续查阅

 

filepath=$(cd `dirname $0`; pwd)

show_usage="args: [-s , -e , -n , -c]\
                                  [--sdate=, --edate=, --numprocs=, --cfile=]"

if [[ -z $@ ]];then
echo $show_usage
exit 0
fi

GETOPT_ARGS=`getopt -o s:e:n:c: -al sdate:,edate:,numprocs:,cfile: -- "$@"`

#echo "$GETOPT_ARGS"
eval set -- "$GETOPT_ARGS"

while [ -n "$1" ]
do
        case "$1" in
                -s|--sdate) sdate=$2; shift 2;;
                -e|--edate) edate=$2; shift 2;;
                -n|--numprocs) numprocs=$2; shift 2;;
                -c|--cfile) cfile=$2; shift 2;;
                --) break ;;
                *) echo $1,$2,$show_usage; break ;;
        esac
done

 

posted @ 2019-03-31 00:04  .狂飙的蜗牛  阅读(4237)  评论(0编辑  收藏  举报