-
Notifications
You must be signed in to change notification settings - Fork 0
/
metric
executable file
·70 lines (52 loc) · 1.39 KB
/
metric
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/bash
set -e
set -o pipefail
################################################################################
function usage {
cat <<EOM
Usage: $(basename "${0}") [-h | --help] <#UN|# UNIT>
-h, --help Show this help message.
EOM
}
function error {
echo "ERROR: ${*}" >/dev/stderr
}
function metric-unit {
case "${unit}" in
mi|mile|miles) echo "km" ;;
mph|"miles per hour") echo "kph" ;;
mi/h) echo "km/h" ;;
in|inch|inches) echo "cm" ;;
ft|feet|foots) echo "m" ;;
yd|yard|yards) echo "m" ;;
ac|acre|acres) echo "m2" ;;
gal|gallon|gallons) echo "l" ;;
lbs|pounds) echo "kg" ;;
*)
error "Unknown unit!"
return 2
;;
esac
}
function split {
local val group
val="${1}"
group="${2}"
sed -E 's/([0-9]*)([a-z][a-z0-9]+)/'"\\${group}"'/' <<< "${val}"
}
function main {
local val unit result metric
case "${#}" in
0) usage; exit 2 ;;
1) val="$(split "${1}" 1)"
unit="$(split "${1}" 2)"
;;
*) val="${1}"
shift
unit="${*}"
esac
metric="$(metric-unit "${unit}")"
result="$(/bin/units -t -o "%.${#val}f" "${val} ${unit}" "${metric}")"
echo "${result} ${metric}"
}
main "${@}"; exit