q.sh/q-weight-convert

20 lines
512 B
Bash
Executable file

#!/bin/bash
#
# Converts weights between pounds and kilograms. Examples:
# 10 pounds in kg
# 2.5 kg in pounds
if [[ -z "$@" ]]; then
echo "^[0-9]+(\.[0-9]+)? kg in pounds$"
echo "^[0-9]+(\.[0-9]+)? pounds in kg$"
elif [[ "$1" == "--dry-run" ]]; then
shift
echo "$@"
else
NUM="$(echo "$@" | sed -E 's/([0-9]+) .*/\1/g')"
if echo "$@" | grep -E "^[0-9]+(\.[0-9]+)? pounds in kg$" > /dev/null; then
awk "BEGIN {printf \"%.2f\n\", $NUM/2.20462}"
else
awk "BEGIN {printf \"%.2f\n\", $NUM*2.20462}"
fi
fi