2002-07-22 15:12:57 +00:00
|
|
|
#!/bin/bash
|
|
|
|
# unit-conversion.sh
|
2012-11-27 14:56:18 +00:00
|
|
|
# Must have 'units' utility installed.
|
2002-07-22 15:12:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
convert_units () # Takes as arguments the units to convert.
|
|
|
|
{
|
|
|
|
cf=$(units "$1" "$2" | sed --silent -e '1p' | awk '{print $2}')
|
|
|
|
# Strip off everything except the actual conversion factor.
|
|
|
|
echo "$cf"
|
|
|
|
}
|
|
|
|
|
|
|
|
Unit1=miles
|
|
|
|
Unit2=meters
|
|
|
|
cfactor=`convert_units $Unit1 $Unit2`
|
|
|
|
quantity=3.73
|
|
|
|
|
|
|
|
result=$(echo $quantity*$cfactor | bc)
|
|
|
|
|
|
|
|
echo "There are $result $Unit2 in $quantity $Unit1."
|
|
|
|
|
|
|
|
# What happens if you pass incompatible units,
|
|
|
|
#+ such as "acres" and "miles" to the function?
|
|
|
|
|
|
|
|
exit 0
|
2012-11-27 14:56:18 +00:00
|
|
|
|
|
|
|
# Exercise: Edit this script to accept command-line parameters,
|
|
|
|
# with appropriate error checking, of course.
|