Debian Codegraph @

unit-conversion.sh

raw symbols SCIP
1 #!/bin/bash
2 # unit-conversion.sh
3 # Must have 'units' utility installed.
4
5
6 convert_units () # Takes as arguments the units to convert.
7 {
8 cf=$(units "$1" "$2" | sed --silent -e '1p' | awk '{print $2}')
9 # Strip off everything except the actual conversion factor.
10 echo "$cf"
11 }
12
13 Unit1=miles
14 Unit2=meters
15 cfactor=`convert_units $Unit1 $Unit2`
16 quantity=3.73
17
18 result=$(echo $quantity*$cfactor | bc)
19
20 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.