I have a function that takes arguments in different combinations. How they are setup depends on other conditions so I created a method to set them up.
#!/bin/bash
myFunc() {
while getopts m:t::T::rp:: arg; do
case "$arg" in
m) log_msg="$OPTARG";;
t) timeout="$OPTARG";;
T) timeout_msg="$OPTARG";;
r) retry="yes";;
p) postpone="$OPTARG";;
*) die "invalid arguments $*";;
esac
done
#doStuff...
}
setupOpts() {
local name="$1"
local param="$2"
local err_msg="$3"
if someTest "$name"; then
# case all
echo "-t 9 -T "$err_msg" -p "$name" -r"
elif someOtherTest "$name" "$param"; then
echo "-t 120 -T "$err_msg" -p "$name""
else
echo "-t 300 -T "$err_msg""
fi
}
mainFunc() {
# some stuff...
opts="$(setupOpts "$someName" "$someParam" "Some Err Msg")"
myFunc -m "Some Msg" "$opts"
}
I not manage to get this working. E.g. when case all (see comment in code) is true:
opts="$(setupOpts "peter-pan" "$someParam" "Some Err Msg")"
# -> results in: myFunc -m "some Msg" -t 9 -T "Some Err Msg" -p "peter-pan" -r
However, the arguments behind -t and following are not parsed corretly. I guess it has something to do with quoting and word splitting.
Depending on quotes:
myFunc -m "Some Msg" "$opts":
timeout=9 -T "Some Err Msg" -p "peter-pan" -r
timeout_msg=
postpone=
retry=
or
myFunc -m "Some Msg" $opts:
timeout=9
timeout_msg=Some
postpone=
retry=
Correct would be
timeout=9
timeout_msg=Some Err Msg
postpone=peter-pan
retry=yes
I have tried different other things like using escaped quotes (echo "-t 9 -T "\"Some Err Msg\"" -p "$argOne" -r") or using echo without surrounding quotes.
Is this somehow doable using bash or am I hitting a limit here?
PS: Setting things up in myFunc would work as well but that's not the solution to my answer. I would like to know if this approach would work with bash somehow.
someTestandsomeOtherTestsetupOpts()should change depending on what condition is true or default to theelsepart. You can use any test for that, e.g. if the name is a certain string.setupOpts()is functioning as expected and the only issue is with the quoting/word-splitting when callingmyFunc()... my answer addresses this issue; alternatively ...bash command line args as arraybrings up many hits on this topic, eg, this and thisbash command line args as arraysince I didn't have that idea and thus posted this question to ask if it is even possible (see PS). And as you can see from the accepted answer simply turning the args into an array doesn't worked, I needed to combine it with a nameref.