Hello all,
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
Quote:
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!
Quote:
#!/bin/bash
#Script for adding todays date and balance to existing file
clear
echo
#User input
echo -n "Todays balance : "
read b;
if test "$b" = ""
then b="Unavailable"
else b="$b"
fi
#Setting variable 'st' to yesterdays balance from text file
st=`cat</home/bob/store `
#Calculating whether increase or decrease in funds
if [[ $b -gt $st ]]
then in=$(( $b - $st ))
h="Increase in balance by "\$$in""
echo "$h"
fi
if [[ $b -lt $st ]]
then in=$(($st - $b ))
h="Decrease in balance by "\$$in""
echo "$h"
fi
if [[ $b -eq $st ]]
then h="Balance remains the same"
echo "$h"
fi
if [[ $b -eq 'Unavailable' ]]
then h="Nothing to calculate."
echo "$h"
fi
#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
echo
#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
z="`date +%p`"
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
cat /home/bob/ncity
exit
|
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?
Thanks!!