배쉬에서, 참조 help getopts
". 옵션 인수, getopts가의 쉘 변수 OPTARG에 인수 장소를 필요로 할 때"
usage() { echo "Usage: $(basename $0) -n name -p port -r"; exit; }
while getopts :n:p:r opt # don't forget the colons for opts that take an arg
do
case $opt in
n) name="$OPTARG" ;;
p) port="$OPTARG" ;;
r) robot=chicken ;;
?) usage ;;
esac
done
shift $((OPTIND - 1))
echo "the name is $name"
echo "the port is $port"
난 당신이 떠들썩한 파티에서 옵션을 구문 분석 할 수있는 솔루션을 주변에 구글 수 있습니다 확신 해요. 다음은 몇 분 노력의 :
#!/bin/bash
usage() { echo foo; exit; }
while [[ $1 == -* ]]; do
case "$1" in
--) shift 1; break ;;
-p|--p|--port) port="$2"; shift 2;;
-n|--n|--name) name="$2"; shift 2;;
*) echo "unknown option: $1"; usage;;
esac
done
echo "the name is $name"
echo "the port is $port"
echo "the rest of the args are:"; (IFS=,; echo "$*")
그리고 테스트,
물론
$ bash longopts.sh --port 1234 --bar a b c
unknown option: --bar
foo
$ bash longopts.sh --port 1234 a b c
the name is
the port is 1234
the rest of the args are:
a,b,c
, 당신이 실제로으로 명령을 호출 '로 myscripf -p 1984 -n someName' –