I have a function to show index of chosen element. I'm trying to pass a parameter to function to use it as an array name. This works:
getIndex() {
arrname=$1[@]
b=("${!arrname}")
index=1; while ((index<${#b[@]})); do
if [[ "${b[$index]}" = "$VALUE" ]]; then
echo "index is $index"; return
fi
((index++)); done
}
But the array, whose name I pass to this function, has index 1 as index of first element (I need this to have indexes similar to line numbers, from which I get patterns in the array:
a=1
while read line; do
if [[ $line =~ ^[0-9] ]]; then
avg[$a]=`echo $line | awk '{print $6}'`
((a++));
fi
And if I'm running function getIndex() the first element of the array starts from index 0.
So, the question is: Is there any way to pass array name in parameter to function with saving indexes of array? Or maybe I just need to forget about it and add +1 to function answer.