I'm for hours trying to figure this one out.
What I would like to do is have a function that can receive N parameters which can include arrays.
Arrays can be in any parameter position, not only the last!
Order of parameters can change, for example, I can pass the first parameter as an array one time and in another call pass as second parameter!
Arrays and normal parameters must be interchangeable!
Example code (Note in this example parameter order is fixed, see following example for problem):
# $1 Menu title
# $2 List of menu options
# $3 Menu text
menu() {
dialog --clear --stdout --title "$1" --menu "$3" 0 0 0 "$2"
}
options=(
1 "Option 1"
2 "Option 2"
3 "Option 3"
4 "Option 4")
menu "Title" ${options[@]} "Text"
The first example can be solved like this:
# $1 Menu title
# $2 List of menu options
# $3 Menu text
menu() {
local -n a=$2
dialog --clear --stdout --title "$1" --menu "$3" 0 0 0 "${a[@]}"
}
options=(
1 "Option 1"
2 "Option 2"
3 "Option 3"
4 "Option 4")
menu "Title" options "Text"
Problem example:
my_func() {
my_command "$1" --something "$2" "$3" --somethingelse "$4"
}
optionsa=(
1 "Option 1"
2 "Option 2"
3 "Option 3"
4 "Option 4")
optionsb=(
1 "Option 1"
2 "Option 2"
3 "Option 3"
4 "Option 4")
my_func "Title" ${optionsa[@]} "Text" ${optionsb[@]}
# Note that I could do this and it must work too:
my_func ${optionsa[@]} "Title" ${optionsb[@]} "Text"
# This too must work:
my_func ${optionsa[@]} ${optionsb[@]} "Title" "Text"
When the array is expanded it must be possible to use the values as parameters to commands, if the value in the array has multiple words quoted ("Option 1") it must be treated as a single parameter to avoid for example passing a path as "/my dir/"
and having it separated as </my> <dir/>
.
How can I solve the second example, where the order or parameter can vary?