Bash Unit conversion
Bash
Download (.zip)
#!/bin/bash # unit-conversion.sh
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
|