셸 스크립트로 매개 변수를 분석하는 템플릿

4469 단어 Bashshellgetopttech

무엇으로


있음getopts, 그러나 긴 옵션을 사용하고 높은 기능getopt을 사용합니다.
GNU 버전과 BSD 버전의 규격은 차이가 있지만 GNU 버전을 따른다.

거푸집


_usage() {
    echo "usage"
    echo "  -a | --arg <str>"
    echo "  -h | --help         Show this message and exit"
}

ARGUMENT="${@}"
option_short="a:dh"
option_long="arg:,debug,help"

OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- ${ARGUMENT})
[[ ${?} != 0 ]] && exit 1
unset option_short option_long
eval set -- "${OPT}"
while :; do
    case ${1} in
        -a | --arg)
	    arg="${2}"
	    shift 2
	    ;;
        -h | --help)
            _usage
            shift 1
	    exit 0
            ;;
        --)
            shift
            break
            ;;
        *)
	    echo "Unknows options ${1}" >&2
            exit 1
            ;;
    esac
done

좋은 웹페이지 즐겨찾기