Issue
I have made the following function to calculate the maximum value of an array:
MaxArray(){
aux ="$1"
for n in "$@"
do
[ "$n" -gt "$aux" ] && aux="$n"
done
return $aux
}
I tried changing return to echo "$aux" to see if it calculated the value correctly and it worked. With an array with the following values: (1, 10, 152, 0, 3), I tried return and called the function like this:
value=$( MaxArray ${array[@]} )
echo "value is $value"
But $value is empty after calling the function. How can I assign the return of the function to $value correctly?
Solution
In the shell, passing values is done through the output, not the return code:
#!/bin/bash
MaxArray() {
local n aux="$1"
for n in "$@"
do
(( n > aux )) && aux=$n
done
echo "$aux"
}
value=$( MaxArray "${array[@]}" )
Answered By - Fravadona Answer Checked By - Dawn Plyler (WPSolving Volunteer)