Saya percaya menggunakan getopts
adalah solusi yang lebih baik untuk penggunaan di muka, ketika Anda membutuhkan fleksibilitas jumlah argumen yang disetujui.
Ini adalah contoh kerja:
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
ref .. http://wiki.bash-hackers.org/howto/getopts_tutorial
KELUARAN:
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