Created
June 3, 2016 04:14
-
-
Save irgeek/882fb4a0b5c74e11a012eb8772adf6c7 to your computer and use it in GitHub Desktop.
Spiffy calculation on the command line!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
spiffy_calc() { | |
local -a FIRST_ARG | |
local -a ACC | |
for var in "$@"; do | |
if [[ ${var} == "in" ]]; then | |
FIRST_ARG=("${ACC[@]}") | |
ACC=() | |
else | |
ACC[${#ACC[@]}]="${var}" | |
fi | |
done | |
if [[ ${#FIRST_ARG[@]} -gt 0 ]]; then | |
gunits -t "${FIRST_ARG[*]}" "${ACC[*]}" | |
else | |
calc="${ACC[@]//p/+}"; | |
calc="${calc//x/*}"; | |
bc -l <<<"scale=5;$calc"; | |
fi | |
} | |
=() { | |
spiffy_calc "${@}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment