I'm having a bit of trouble with a script. I wrote this script that lets me add the daily balance from a monetary fund and it works good. What I want to do now is read 'todays' input and compare it to 'yesterdays' input and calculate whether it was an increase or a decrease. I've got it to the point where it will calculate it, as long as there are no decimals in the number.
It will calculate 11,312-11,206 and tells me that the balance decreased by $112. I'm looking for more accurate numbers so I need to be able to calculate floating point. The problem is with loading the variables 'b' and 'st' in the script. As long as it is an integer and doesn't contain a decimal everything is fine but the daily balance always has two digits trailing a decimal and then I get an error message. I ran the script and put in an arbitrary number--12,342.56
05:51 PM /scpt : bank.bk
Todays balance : 12342.56
/scpt/bank.bk: line 15: [[: 12342.56: syntax error in expression (error token is ".56")
/scpt/bank.bk: line 20: [[: 12342.56: syntax error in expression (error token is ".56")
/scpt/bank.bk: line 25: [[: 12342.56: syntax error in expression (error token is ".56")
/scpt/bank.bk: line 29: [[: 12342.56: syntax error in expression (error token is ".56")
Here's what I've written. Forgive the ugliness, I'm new to scripting!
#Script for adding todays date and balance to existing file
echo -n "Todays balance : "
if test "$b" = ""
#Setting variable 'st' to yesterdays balance from text file
#Calculating whether increase or decrease in funds
if [[ $b -gt $st ]]
then in=$(( $b - $st ))
h="Increase in balance by "\$$in""
if [[ $b -lt $st ]]
then in=$(($st - $b ))
h="Decrease in balance by "\$$in""
if [[ $b -eq $st ]]
then h="Balance remains the same"
if [[ $b -eq 'Unavailable' ]]
then h="Nothing to calculate."
#Removing file containing yesterdays balance
rm -f /home/bob/store
#Re-creating file containing todays balance for use tomorrow to be used by variable 'st'
echo "$b" >> /home/bob/store;chmod 755 /home/bob/store
#Sending input to file that keeps track of every days balance
#This is ugly. I'm sure there is a quicker and neater way to do this. I'll figure it out.
#Setting am/pm to uppercase
echo "`date +%m-%d-%y`" "\$$b" "`date +%l:%M`"" $z $h " >> /home/bob/add
cat /home/bob/ncity /home/bob/add > /home/bob/n2;
rm -f /home/bob/ncity;
sort -n /home/bob/n2 -o /home/bob/n2
cp /home/bob/n2 /home/bob/ncity
chmod 755 /home/bob/ncity
rm -f /home/bob/n2
rm -f /home/bob/add
cp -rpb /home/bob/ncity /home/bob/.ncity/ncity.bak
I've done a little reading in a manual on Bash concerning 'bc' and floating point but I'm having difficulty understanding how to implememt it into a script. Anyone have a suggestion?