나는 사용을 믿는다. getopts
전달 된 인수의 양에 유연성이 필요할 때 사전 사용을위한 더 나은 솔루션입니다.
이것은 실제 예입니다.
if (($# == 0)); then
echo "Please pass argumensts -p <pkg1><pkg2>... -m <email1><email2>.."
exit 2
fi
while getopts ":p:m:" opt; do
case $opt in
p)
echo "-p was triggered, Parameter: $OPTARG" >&2
PKGS=$OPTARG
;;
m)
echo "-m was triggered, Parameter: $OPTARG" >&2
MAIL=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
echo "go thru selection"
for PKG in $PKGS;
do
echo "ARG_PKG: $PKG"
done
echo "go thru selection email"
for M in $MAIL;
do
echo "ARG_MAIL: $M"
done
exit 0
참조 .. http://wiki.bash-hackers.org/howto/getopts_tutorial
산출:
bash t -p "pkg1 pkg2 pkg3" -m "bobo@m.com boba@m.com"
-p was triggered, Parameter: pkg1 pkg2 pkg3
-m was triggered, Parameter: bobo@m.com boba@m.com
go thru selection
ARG_PKG: pkg1
ARG_PKG: pkg2
ARG_PKG: pkg3
go thru selection email
ARG_MAIL: bobo@m.com
ARG_MAIL: boba@m.com