Я предлагаю вам взглянуть на мой Общий сценарий оболочки на GitHub: utility_functions.sh . Там вы увидите функцию getArgs . Он предназначен для связывания параметров и значений.
Я вставляю сюда только функцию, но это зависит от пары дополнительных функций внутри этого скрипта
##########################
#
# Function name: getArgs
#
# Description:
# This function provides the getopts functionality
# while allowing the use of long operations and list of parameters.
# in the case of a list of arguments for only one option, this list
# will be returned as a single-space-separated list in one single string.
#
# Pre-reqs:
# None
#
# Output:
# GA_OPTION variable will hold the current option
# GA_VALUE variable will hold the value (or list of values) associated
# with the current option
#
# Usage:
# You have to source the function in order to be able to access the GA_OPTIONS
# and GA_VALUES variables
# . getArgs $*
#
####################
function getArgs {
# Variables to return the values out of the function
typeset -a GA_OPTIONS
typeset -a GA_VALUES
# Checking for number of arguments
if [[ -z $1 ]]
then
msgPrint -warning "No arguments found"
msgPrint -info "Please call this function as follows: . getArgs \$*"
exit 0
fi
# Grab the dash
dash=$(echo $1 | grep "-")
# Looking for short (-) or long (--) options
isOption=$(expr index "$dash" "-")
# Initialize the counter
counter=0
# Loop while there are arguments left
while [[ $# -gt 0 ]]
do
if [[ $dash && $isOption -eq 1 ]]
then
(( counter+=1 ))
GA_OPTIONS[$counter]=$1
shift
else
if [[ -z ${GA_VALUES[$counter]} ]]
then
GA_VALUES[$counter]=$1
else
GA_VALUES[$counter]="${GA_VALUES[$counter]} $1"
fi
shift
fi
dash=$(echo $1 | grep "-")
isOption=$(expr index "$dash" "-")
done
# Make the variables available to the main algorithm
export GA_OPTIONS
export GA_VALUES
msgPrint -debug "Please check the GA_OPTIONS and GA_VALUES arrays for options and arguments"
# Exit with success
return 0
}
Как видите, эта конкретная функция будет экспортировать GA_OPTIONS и GA_VALUES. Единственным условием для этого является то, что значения должны быть списком, разделенным пробелами, после опции.
Вы могли бы назвать скрипт как ./ tool --var1 2 3 4 5 --var2 = "6" --var3 = "7"
Или просто используйте аналогичную логику в соответствии с вашими предпочтениями .