#!/bin/sh # The MIT License (MIT) # # Copyright © 2026 pacman64 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the “Software”), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # bunits [options...] [quantity/unit pairs...] # # Better UNITS is an easier way to use the `units` command-line app, with a # focus on shortcuts for converting into international units. # # Several unit shortcuts are available, including # # ac, acre, acres # cup, cups # day, days # deg, degs, degree, degrees # f, fahr, fahren, fahrenheit, fahrenheits # floz, # ft, feet, foot, ft2, ft^2, sqft, sqfeet, ft3, ft^3, cuft, cufeet # gal, gallon, gallons # gb, gib, gibi, gibibytes # hr, hour, hours # in, inch, inches # kb, kib, kibi, kibibytes # lb, lbs, pound, pounds # mb, mib, mibi, mibibytes # mi, mile, miles, mi2, mi^2, miles^2, sqmi, sqmile, sqmiles # mi3, mi^3, miles^3, cumi, cumile, cumiles # min, minute, minutes # mph # nmi, nmile, nmiles, nmi2, nmi^2, nmile^2, nmiles^2 # oz, ozs, ounce, ounces # pt, pts, pint, pints, uspint, uspints # tb, tib, tibi, tibibytes # yd, yds, yard, yards, yd2, yds2, yd^2, yds^2, yard^2, yards^2 # wk, week, weeks # # The options are, available both in single and double-dash versions # # -h, -help show this help message case "$1" in -h|--h|-help|--help) awk '/^# +bunits /, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 ;; esac [ "$1" = '--' ] && shift if [ $# -lt 2 ]; then awk '/^# +bunits /, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 fi conv="units -v -H ''" while [ $# -ge 2 ]; do # unit="$(echo "$2" | awk '{ print tolower($0) }')" unit="$(echo "$2" | tr '[A-Z]' '[a-z]')" case "${unit}" in ac|acre|acres) ${conv} "$1 acres" kilometers^2;; cup|cups) ${conv} "$1 cups" liters;; day|days) ${conv} "$1 days" seconds;; deg|degs|degree|degrees) ${conv} "$1 degrees" radians;; f|fahr|fahren|fahrenheit|fahrenheits) ${conv} "tempF($1)" tempC;; floz) ${conv} "$1 floz" milliliters;; ft|feet|foot) ${conv} "$1 feet" meters;; ft2|ft^2|sqft|sqfeet) ${conv} "$1 ft^2" meters^2;; ft3|ft^3|cuft|cufeet) ${conv} "$1 ft^3" meters^3;; gal|gallon|gals|gallons) ${conv} "$1 gallons" liters;; gb|gib|gibi|gibibytes) ${conv} "$1 gibibytes" bytes;; hr|hour|hours) ${conv} "$1 hours" seconds;; in|inch|inches) ${conv} "$1 inches" centimeters;; kb|kib|kibi|kibibytes) ${conv} "$1 kibibytes" bytes;; lb|lbs|pound|pounds) ${conv} "$1 pounds" kilograms;; mb|mib|mibi|mibibytes) ${conv} "$1 mibibytes" bytes;; mi|mile|miles) ${conv} "$1 miles" kilometers;; min|minute|minutes) ${conv} "$1 minutes" seconds;; mi2|mi^2|miles^2|sqmi|sqmile|sqmiles) ${conv} "$1 mi^2" kilometers^2;; mi3|mi^3|miles^3|cumi|cumile|cumiles) ${conv} "$1 mi^3" kilometers^3;; mph) ${conv} "$1 mph" kph;; nmi|nmile|nmiles) ${conv} "$1 nmi" kilometers;; nmi2|nmi^2|nmile^2|nmiles^2) ${conv} "$1 nmi^2" kilometers^2;; oz|ozs|ounce|ounces) ${conv} "$1 ounces" grams;; pt|pts|pint|pints|uspint|uspints) ${conv} "$1 uspints" liters;; tb|tib|tibi|tibibytes) ${conv} "$1 tibibytes" bytes;; yd|yds|yard|yards) ${conv} "$1 yards" meters;; yd2|yds2|yd^2|yds^2|yard^2|yards^2) ${conv} "$1 yards^2" meters^2;; wk|wks|week|weeks) ${conv} "$1 weeks" seconds;; *) printf "unsupported unit shortcut '%s'\n" "${unit}" >&2; exit 1;; esac shift shift done if [ $# -eq 1 ]; then printf "you forgot the unit for the last value\n" >&2 exit 1 fi