mirror of https://github.com/tLDP/LDP
29 lines
680 B
Bash
29 lines
680 B
Bash
#!/bin/bash
|
|
# unit-conversion.sh
|
|
# Must have 'units' utility installed.
|
|
|
|
|
|
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
|
|
|
|
# Exercise: Edit this script to accept command-line parameters,
|
|
# with appropriate error checking, of course.
|