使用getopt
示例如下:
TEMP=`getopt -o j: --long job:,install -- "$@"` eval set -- "$TEMP" INSTALL= PACKAGE= JOB_NUM=$(nproc) while true ; do case "$1" in --install) INSTALL="ON"; shift ;; -j | --job) JOB_NUM=$2; shift 2;; --) shift ; break ;; *) echo "Internal error!" ; exit 1 ;; esac done # echo "options:INSTALL("$INSTALL"),PACKAGE("$PACKAGE"),JOB_NUM("$JOB_NUM")"
:
或者::
, 前者表示一定有值,后者表示可有值,或没有值-l
,多个选项之间用,
分割j:
: 表示选项j
一定要带值install
: 表示不带选项