LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-09-2006, 05:15 PM   #1
Freestone
Member
 
Registered: Jan 2003
Location: Brighton, Michigan
Distribution: FC5
Posts: 114

Rep: Reputation: 15
Trouble with script and bc


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!!
 
Old 10-09-2006, 08:35 PM   #2
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Rep: Reputation: 52
Here's a way:

Code:
string=$(echo "10.5 * .80" | bc)
echo $string
-twantrd
 
Old 10-11-2006, 09:34 AM   #3
Freestone
Member
 
Registered: Jan 2003
Location: Brighton, Michigan
Distribution: FC5
Posts: 114

Original Poster
Rep: Reputation: 15
For anyone intersted, I figured it out and I'm supplying the answer. I m sure there is more than one
way to do what I was trying to do!!!

I was trying to calculate numbers with decimals in them and then cat the results into a file each day.
I kept getting errors, ie. 'integer expected'.

If I entered a number into the script such as 11299.43 and wanted it directed into the variable 'st', this is what I did:
Quote:
input your number
read b
#Getting rid of floating point
st=$(echo "scale=0; "$b"/.01" | bc )
# Now performed calculation such as if [ $st -lt '100' ]; then blah blah; fi
#Convert back to decimal
st=$(echo; "scale=2; "$b"*.01" | bc)
#scale=2 gives us two digits to the right of the decimal
Folks, thats what I was looking for. I just had to keep digging.
 
Old 10-11-2006, 12:07 PM   #4
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Rep: Reputation: 52
Quote:
Folks, thats what I was looking for. I just had to keep digging.
Had to keep digging? Why? I gave you the answer and it looks like you used it.

-twantrd
 
Old 10-11-2006, 06:10 PM   #5
Freestone
Member
 
Registered: Jan 2003
Location: Brighton, Michigan
Distribution: FC5
Posts: 114

Original Poster
Rep: Reputation: 15
twantrd,

Thanks for your reply. But unfortunately, you didn't supply an answer that actually works.
When a variable is set to a number, it has to be an integer in order to run it through something
like 'if [ $b -lt $st ]. I was getting errors left and right and all I had to do was use 'bc' to get rid of
the floating point, then set the variables, do the math and then re-introduce the floating point.\
This script accepts input in the form of todays bank balance. It reads yesterdays balance that is stored
in a text file ( which has a decimal point in it) and checks to see whether it was an increase, decrease or
no change in balance ( hence the 'if [ $b -gt $st ]; then bt=$(( $b - $st ));fi etc.) The 'if-then' construct only seems to accept variables that are integers. I had to convert everything to integers.

Make any sense? I hope so.

Thanks again for your input. And your previous post actually helped me 'keep digging'.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble with my first SlackBuild script Yalla-One Slackware 6 06-10-2006 02:03 PM
having trouble with a intall script dexterwy Linux - Software 1 06-03-2005 10:22 AM
cgi script trouble Michele Programming 5 05-14-2004 03:23 PM
Trouble in Shell Script Gerardoj Linux - General 6 12-02-2003 09:21 PM
imwheel and script trouble Toker Linux - General 8 10-06-2003 02:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration