| 1 | #!/bin/bash |
| 2 | # sum-product.sh |
| 3 | # A function may "return" more than one value. |
| 4 | |
| 5 | sum_and_product () # Calculates both sum and product of passed args. |
| 6 | { |
| 7 | echo $(( $1 + $2 )) $(( $1 * $2 )) |
| 8 | # Echoes to stdout each calculated value, separated by space. |
| 9 | } |
| 10 | |
| 11 | echo |
| 12 | echo "Enter first number " |
| 13 | read first |
| 14 | |
| 15 | echo |
| 16 | echo "Enter second number " |
| 17 | read second |
| 18 | echo |
| 19 | |
| 20 | retval=`sum_and_product $first $second` # Assigns output of function. |
| 21 | sum=`echo "$retval" | awk '{print $1}'` # Assigns first field. |
| 22 | product=`echo "$retval" | awk '{print $2}'` # Assigns second field. |
| 23 | |
| 24 | echo "$first + $second = $sum" |
| 25 | echo "$first * $second = $product" |
| 26 | echo |
| 27 | |
| 28 | exit 0 |