I have a script that has a couple of arrays in it. I didn't want to repeat counting the array size so I tried to create a function that does it for me. I'm passing the function the name of the array whose size I want. It runs through the function until it finds an empty space and then returns the count. This is my function:
array_size ()
{
# Calculate the size of your array
count=0
while [ ${$1[$count]} != "" ]
do
count=$(( $count + 1 ))
done
#Subtracts 1 from count to reset array position
count=$(( $count - 1 ))
return $count
}
I get the following error when I call this: ${$1[$count]}: bad substitution. How can I pass the array name to this function?
('' 1 2 3)would be considered to have length zero by your function.arr[1]=xhas no index 0.