Register
It is currently Sat Oct 25, 2014 3:26 am

Simple financial records script


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Tue Oct 10, 2006 6:19 pm   

Joined: Tue Oct 10, 2006 5:44 pm
Posts: 21
Location: Brighton, Michigan
Greetings. This is my first post so if I am posting this in the wrong forum or location, please direct me to the apropo place.

I have been dabbling in bash scripting for a few months and I've really started to enjoy it. I have an account I check daily online and I wanted something that would keep track of yesterdays balance, accept input for todays balance, compare the two and store them in separate files. I was struggling with calculating numbers with decimals in them but I found a way around it.
I'm posting this script for others to look at and if somebody or anybody has any input, suggestions or shortcuts I welcome them!! I am learning and I am having a blast making my laptop do what I tell it to do.

If there is something in this script that looks goofy its because I'm new to this. I'm just looking for others opinions....Here it is:

Code:
#!/bin/bash
#Script for inputting daily balance from fund
#and comparing it to the previous days balance
#and reporting whether it was an increase or decrease.
# Setting date and current time variables
dt=`date +%B-%d-%Y`;ct=`date +%I:%M\ %P`;dtb="\b\b\b\b";ctb="             "
clear
echo    " _____________________________________ "
echo    "|                                     |"
echo    "|            Greetings!!              |"
echo    "|                                     |"
echo    "|             Today is                |"
echo    "|                                     |"
echo    "|          "$dt"                "$dtb"|"
echo    "|        _ _            _ _           |"
echo    "|        _*_  "$ct"  _*_           |"
echo    "|_____________________________________|"   
echo
#
# Loading yesterday's balance from text file into variable
# 'st' will represent /home/bob/store--the file that holds
# the previous days balance from the fund.
#
st=`cat</home/bob/store`
rm -f /home/bob/store
echo
echo -n "Today's balance:  \$"
read b
echo
echo -e "Previous balance: \$$st"
echo -e "Current balance:  \$$bt"
#
#Getting rid of floating point so the calculations can
#be done without errors
#
b=$(echo "scale=0; "$b"/.01" | bc)
st=$(echo "scale=0; "$st"/.01" | bc)
#
#Resetting floating point for appearance's sake
#in the reference file for variable 'str'
#
str=$(echo "set=5;scale=5;"$b"*.01" | bc)
#
#Comparing and calculating yesterday and todays #'s
#
if [ $b -gt $st ]
then bi=$(( $b - $st ))
bi=$(echo "set=5;scale=2; "$bi"*.01" | bc)
report="Increase of \$$bi"
fi
if [ $b -lt $st ]
then bd=$(( $st - $b ))
bd=$(echo "set=5;scale=2; "$bd"*.01" | bc)
report="Decrease of \$$bd"
fi
if [ $b == $st ]
then bnc="$st"
bnc=$(echo "set=5;scale=2; "$bnc"*.01" | bc)
report="Nocrease of  \$$bnc"
fi
echo
echo "$report"
#
# Just for fun. A spinner!! Borrowed from     
# bashscripts.org but modified just a bit
#
echo
function spin {
echo -ne '-\b'
sleep .02
echo -ne '|\b'
sleep .02
echo -ne '/\b'
sleep .02
echo -ne '-\b'
sleep .02
echo -ne '|\b'
sleep .02
echo -ne '\\\b'
sleep .02
echo -ne '+'
}
for i in `seq 1 20` ; do spin; done
echo "";
#
#Storing todays balance in a file for use
#tomorrow
#
echo "$str" >> /home/bob/store; chmod 775 /home/bob/store
#
#Storing the results in another file for reference
#and comparison of each days balance
#
echo "`date +%m-%d-%y`" "\$$str" " `date +%l:%M\ %p`" " $report" >> /home/bob/add
cat /home/bob/ncity /home/bob/add > /home/bob/n2
rm -f /home/bob/ncity
cp -rpb /home/bob/n2 /home/bob/ncity
chmod 775 /home/bob/ncity
rm -f /home/bob/n2
rm -f /home/bob/add
cp -rpb /home/bob/ncity /home/bob/.ncity/ncity.bak
#
#Viewing the results
#
cat /home/bob/ncity | tail -n 5
exit


Thanks for any input!!

Freestone


Top
 Profile  
 PostPosted: Wed Oct 11, 2006 9:14 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
first off... welcome to bashscripts.org
second... good job on the script!

it looks well thought out. there are ofcourse some adjustments you could make to shorten the line count, and some redundancies... but all that is rather trivial. all in all it looks like something useful.

good job :)


Top
 Profile  
 PostPosted: Wed Oct 11, 2006 4:46 pm   

Joined: Tue Oct 10, 2006 5:44 pm
Posts: 21
Location: Brighton, Michigan
Thanks for your reply jbsnake!

I am experiencing one problem with the output of this script and I am in search of a solution.

Here is a sample output of the script:

Quote:

Previous balance: $21443.12

Today's balance: $21443.12

Nocrease of $-----

++++++++++++++++++++
10-11-06 $14566.11 6:24 PM Increase of $3263.15
10-11-06 $.07 6:24 PM Decrease of $14566.04
10-11-06 $11965.87 6:25 PM Increase of $11965.80
10-11-06 $21443.12 6:28 PM Increase of $9477.25
10-11-06 $21443.12 6:28 PM Nocrease of $-----
06:28 PM /scpt :





The issue above is where the balance is $.07. The line is truncated and I want all the '$', 'times', 'Increase' and
'Decrease' to all line up but I don't have a clue. I messed around with the 'bc' command, trying to alter 'scale', 'set' and 'length' (all that 'length' did was give me a parse error). A better way to put it is I want the decimal to always line up in the output regardless of how many digits there are to the left of it. I need to keep learning here and I'm open...

Any ideas?

Thank you!


Top
 Profile  
 PostPosted: Wed Oct 11, 2006 9:14 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 669
Location: Des Moines, Iowa
Code:
printf "%8s %10s %4s %2s %7s %2s %10s\n" 10-11-06 \$.07 6:24 PM Decrease of \$14566.04


#!/bin/bash
# Formatting test
printf "%8s %10s %4s %2s %7s %2s %10s\n" 10-11-06 \$.07 6:24 PM Decrease of \$14566.04 ;
printf "%8s %10s %4s %2s %7s %2s %10s\n" 10-11-06 \$11965.87 6:24 PM Decrease of \$11965.80 ;
printf "%8s %10s %4s %2s %7s %2s %10s\n" 10-11-06 \$21443.12 6:24 PM Decrease of \$9477.25 ;
echo "done"
exit 0


RESULTS IN:

crouse@linux-5dr8:~/Desktop> sh formattest.sh
10-11-06       $.07 6:24 PM Decrease of  $14566.04
10-11-06  $11965.87 6:24 PM Decrease of  $11965.80
10-11-06  $21443.12 6:24 PM Decrease of   $9477.25
done
crouse@linux-5dr8:~/Desktop>       



You could use the print f command to format the text better..... It would be easiest if the output was variables....but i put in the text for clarity.....


Top
 Profile WWW  
 PostPosted: Thu Oct 12, 2006 9:58 am   

Joined: Tue Oct 10, 2006 5:44 pm
Posts: 21
Location: Brighton, Michigan
crouse,

I'm totally lost concerning your reply. :lol:

You said it would be easier if the output was variables...
A portion of the code:
Code:
# Loading yesterday's balance from text file into variable
# 'st' will represent /home/bob/st--the file that holds
# the previous days balance from the fund.
st=`cat</home/bob/st`
echo -e "Previous balance: \$$st"
echo
echo -n "Today's balance:  \$"
read b
b=$(echo "scale=0; "$b"/.01" | bc)
st=$(echo "scale=0; "$st"/.01" | bc)
str=$(echo "scale=2; "$b"*.01" | bc)
if [ $b -gt $st ]
then bi=$(( $b - $st ))
bi=$(echo "scale=2; "$bi"*.01" | bc)
report="Increase of  \$$bi"
fi
if [ $b -lt $st ]
then bd=$(( $st - $b ))
bd=$(echo "scale=2; "$bd"*.01" | bc)
report="Decrease of  \$$bd"
fi
if [ $b == $st ]
then
report="Nocrease of  \$-----"
fi



And where the results get directed to a text file...
Code:
echo "$str" >> /home/bob/st; chmod 775 /home/bob/st
echo -e "`date +%m-%d-%y`" "\$$str" " `date +%l:%M\ %p`" " $report" >> /home/bob/add
cat /home/bob/ncity /home/bob/add > /home/bob/n2


I thought I was using variables. Are you saying that I should use the 'print' command instead of the 'echo' command? Like I said in my original post, I'm new to scripting and I'm learning.
I have a few different bash tutorials that I've downloaded, so in this situation do I read up on formatting text?

Thanks so much.


Top
 Profile  
 PostPosted: Thu Oct 12, 2006 2:29 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 669
Location: Des Moines, Iowa
Freestone wrote:
crouse,

I'm totally lost concerning your reply. :lol:

You said it would be easier if the output was variables...
A portion of the code:
Code:
# Loading yesterday's balance from text file into variable
# 'st' will represent /home/bob/st--the file that holds
# the previous days balance from the fund.
st=`cat</home/bob/st`
echo -e "Previous balance: \$$st"
echo
echo -n "Today's balance:  \$"
read b
b=$(echo "scale=0; "$b"/.01" | bc)
st=$(echo "scale=0; "$st"/.01" | bc)
str=$(echo "scale=2; "$b"*.01" | bc)
if [ $b -gt $st ]
then bi=$(( $b - $st ))
bi=$(echo "scale=2; "$bi"*.01" | bc)
report="Increase of  \$$bi"
fi
if [ $b -lt $st ]
then bd=$(( $st - $b ))
bd=$(echo "scale=2; "$bd"*.01" | bc)
report="Decrease of  \$$bd"
fi
if [ $b == $st ]
then
report="Nocrease of  \$-----"
fi



And where the results get directed to a text file...
Code:
echo "$str" >> /home/bob/st; chmod 775 /home/bob/st
echo -e "`date +%m-%d-%y`" "\$$str" " `date +%l:%M\ %p`" " $report" >> /home/bob/add
cat /home/bob/ncity /home/bob/add > /home/bob/n2


I thought I was using variables. Are you saying that I should use the 'print' command instead of the 'echo' command? Like I said in my original post, I'm new to scripting and I'm learning.
I have a few different bash tutorials that I've downloaded, so in this situation do I read up on formatting text?

Thanks so much.



Sorry..... i was in a hurry last night when i wrote the reply...

Yes.... I'm saying to use "print f" instead of "echo" so you will have some control over your formatting when displaying on the terminal. I was also saying that i wasn't using variables when printing...... i was using plain text..... I meant that YOU should use variables where I was using Plain Text ;)

Hope that's a bit clearer ...

http://www.bashscripts.org/docs/Advance ... ernal.html
look for printf on that page.....has some examples.


I'd also suggest reading from your terminal

info printf
man printf

and on the net......
http://www.gnu.org/software/coreutils/m ... ls_15.html
http://www.ss64.com/bash/printf.html


Top
 Profile WWW  
 PostPosted: Thu Oct 12, 2006 5:10 pm   

Joined: Tue Oct 10, 2006 5:44 pm
Posts: 21
Location: Brighton, Michigan
Thanks for clearing it up. I have plenty of reading to do now! I'll work on it.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP