使用 getopt 获取参数
# ./example.sh abc.tar.gz -u username -p password --port=8080
FILE=""
if [ "X$1" != "X" ];then
FILE=$1
if [ ! -f "$FILE" ]; then
"${FILE} not exist!"
exit
fi
else
echo -e "\e[0;32m parameter missing \e[0m"
fi
port=0
userName=""
password=""
set -- $(getopt -u -o u:p: -l port: "$@") # -o 接收短参数, -l 接收长参数, 需要参数值的在参数后面添加:
while [ -n "$1" ]; do
case "$1" in
-u) userName=$2
shift ;;
-p) password=$2
shift ;;
--port)
port=$2
shift
esac
shift
done
echo "${userName}--${password}--port:${port}"