Error while passing array as function parameter in bash -
this code dealing with:
function execute { task="$1" servername="$2" "$task" "${servername[@]}" } function someotherthing { val=$1 echo "$val" } function makenecessarydirectory { arr=("$@") echo "${arr[@]}" } dem=(1 2 3 4 5) execute someotherthing 1 execute makenecessarydirectory "${dem[@]}"
output:
1 1
expected output:
1 1 2 3 4 5
how achieve this? found no error logically.
side question
:
is safe receive 2nd parameter array inside execute
can deal both of dependent functions, or should have explicit check inside execute
?
as explained in comment
you passing array individual args execute
, passing first 1 makenecessarydirectory
, $@
single argument passed 1.
i way, have added comments parts have changed. minor changes should work you.
#!/bin/bash function execute { task="$1" servername="$2" "$task" "$servername" #no longer pass array here pass servername name of array } function someotherthing { val=$1 echo "$val" } function makenecessarydirectory { local arr=("${!1}") #indirect reference first arg passed function #name of array in "${arr[@]}"; echo "$i" done } dem=(1 2 3 4 5) execute someotherthing 1 execute makenecessarydirectory 'dem[@]' #pass array name instead of it's contents
Comments
Post a Comment