Skip to content

Instantly share code, notes, and snippets.

@Gen2ly
Created June 1, 2012 12:15
Show Gist options
  • Save Gen2ly/2851690 to your computer and use it in GitHub Desktop.
Save Gen2ly/2851690 to your computer and use it in GitHub Desktop.
Command line calculator
#!/bin/sh
# Command line calculator
# Display usage if no parameters given
if [ -z "$@" ]; then
echo " ${0##*/} <input> <*decimals> - command line calculator (-h for examples)"
exit
fi
# Decimal to be carried out to (uses four unless value is given)
if [ -z "$2" ]; then
decimals=4; else
decimals=$2
fi
case $1 in
-h | --help ) echo " Requires quotes. Examples:"
echo ' calc "5*((2^3*2)/1.352)"'
echo ' calc "p=10; n=9; p*n+10"' ;;
* ) echo "scale=$decimals;$1" | bc
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment