-
Notifications
You must be signed in to change notification settings - Fork 1
/
calc
executable file
·41 lines (32 loc) · 967 Bytes
/
calc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/bash
#A simple script to do floating point maths
#Author: David Dickinson
#Date: 22/06/2010
#Updated: 30/05/2012
#Define expected number of arguements
EXPECTED_ARGS=1
#Check we get sufficient parameters
if [ ! $# -eq $EXPECTED_ARGS ]
then
echo "Error: recieved "$#" args when expected exactly "$EXPECTED_ARGS
echo $0 "usage: "$0" calculation"
echo "Tip: Consider wrapping your command in quotes!"
exit 1
fi
#Split options string
NPAR=`echo ${1} | awk -F";" '{print NF}'`
if [ ${NPAR} -eq 1 ]
then
VAL=${1}
OPT=""
else
VAL=`echo ${1} | awk -F";" '{print $2}'`
OPT=`echo ${1} | awk -F";" '{print $1}'`"; "
fi
#Handle continued lines
VAL=`echo ${VAL} | sed ':a;N;$!ba;s/\\\n//g' | sed 's/\\\ //g'`
#Handle exponential notation
VAL=`echo ${VAL} | sed 's/[eEdD]\([-+0-9]*\)/\*10\^(0\1)/g'`
#ECHO calculation to bc and make sure normal formatting used!
echo "scale=8; ${OPT}${VAL}" | bc | sed 's/\-\./\-0\./g' | sed 's/^\./0./g'
exit 0