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