Acredito que usar getopts
é uma solução melhor para uso antecipado, quando você precisar de flexibilidade na quantidade de argumentos transmitidos.
Este é um exemplo prático:
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 .. link
OUTPUT:
bash t -p "pkg1 pkg2 pkg3" -m "[email protected] [email protected]"
-p was triggered, Parameter: pkg1 pkg2 pkg3
-m was triggered, Parameter: [email protected] [email protected]
go thru selection
ARG_PKG: pkg1
ARG_PKG: pkg2
ARG_PKG: pkg3
go thru selection email
ARG_MAIL: [email protected]
ARG_MAIL: [email protected]