Skip to content

Instantly share code, notes, and snippets.

@irgeek
Created June 3, 2016 04:14
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save irgeek/882fb4a0b5c74e11a012eb8772adf6c7 to your computer and use it in GitHub Desktop.
Spiffy calculation on the command line!
#!/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