| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | # unit-conversion.sh |
| 3 | # Must have 'units' utility installed. |
| 4 | |
| 5 | |
| 6 | convert_unitsscip-shell shell abs-guide 10-4 convert_units(). () # Takes as arguments the units to convert. |
| 7 | { |
| 8 | cf=$(units "$1" "$2" | sedscip-shell system . . sed. --silent -e '1p' | awkscip-shell system . . awk. '{print $2}') |
| 9 | # Strip off everything except the actual conversion factor. |
| 10 | echoscip-shell system . . echo. "$cf" |
| 11 | } |
| 12 | |
| 13 | Unit1=miles |
| 14 | Unit2=meters |
| 15 | cfactor=`convert_unitsscip-shell shell abs-guide 10-4 convert_units(). $Unit1 $Unit2` |
| 16 | quantity=3.73 |
| 17 | |
| 18 | result=$(echoscip-shell system . . echo. $quantity*$cfactor | bc) |
| 19 | |
| 20 | echoscip-shell system . . echo. "There are $result $Unit2 in $quantity $Unit1." |
| 21 | |
| 22 | # What happens if you pass incompatible units, |
| 23 | #+ such as "acres" and "miles" to the function? |
| 24 | |
| 25 | exit 0 |
| 26 | |
| 27 | # Exercise: Edit this script to accept command-line parameters, |
| 28 | # with appropriate error checking, of course. |