Register
It is currently Thu Feb 22, 2018 10:34 am

Using bash as a calculator


All times are UTC


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Thu Mar 16, 2006 10:11 pm   
Site Admin
User avatar

Joined: Thu Apr 17, 2003 3:44 am
Posts: 12300
Location: Iowa
bash doesn't work with decimals very well, so in order to overcome that on the shell for just normal routine calculations, I wrote a script and used an alias to overcome bash's limitations using bc.

Code:
#!/bin/bash
echo "scale=4; $1" | bc ;exit

save as bashcalc.sh then put an alias into your .bashrc file
Code:
alias calc='sh /home/crouse/scripts/bashcalc.sh'


Quote:
[email protected]:~> calc 3.555+7.999
11.554
[email protected]:~>

_________________

Zbook (Laptop)- Mint 18 64-bit - I7-4800mq - 32GB ram
Phantom (Desktop)- Mint 64-bit - i7-5770 - 32GB ram
Nas4Free (NAS) - 6X3TB File Server.
Sky (HP BladeCenter) - 16 blades - 536 GB ram-G7/G6/G5 blades.


Top
 Profile WWW  
 PostPosted: Fri Mar 17, 2006 6:42 am   
Sr. Member

Joined: Sun Dec 12, 2004 6:08 am
Posts: 1200
Location: Cupertino, CA
Nifty little trick there Crouse.

Just to expand on the concept of math from the console....
Python makes also makes a great "desktop calculator". Just type "python" at the command prompt and type in whatever mathematical expression and press enter.


Top
 Profile  
 PostPosted: Fri Mar 17, 2006 6:59 am   
Site Admin
User avatar

Joined: Thu Apr 17, 2003 3:44 am
Posts: 12300
Location: Iowa
yep........ i just wanted the same in bash ;)

_________________

Zbook (Laptop)- Mint 18 64-bit - I7-4800mq - 32GB ram
Phantom (Desktop)- Mint 64-bit - i7-5770 - 32GB ram
Nas4Free (NAS) - 6X3TB File Server.
Sky (HP BladeCenter) - 16 blades - 536 GB ram-G7/G6/G5 blades.


Top
 Profile WWW  
 PostPosted: Mon Mar 27, 2006 8:15 am   
New Member

Joined: Fri Mar 17, 2006 5:19 pm
Posts: 34
thanks for this tip.


Top
 Profile WWW  
 PostPosted: Mon Jun 19, 2006 7:04 pm   
Site Admin
User avatar

Joined: Thu Apr 17, 2003 3:44 am
Posts: 12300
Location: Iowa
I updated my older bashcalc script.... made it more useful to myself..... what i use it for day to day.....

May or may not be useful to anyone else......

Code:
#!/bin/bash

#######################################################
#     Bash CALCulator
#######################################################
#
#
#    FILE: bashcalc2.sh
# VERSION: 1.1
#    DATE: 06-19-2006
#
#  AUTHOR: Crouse - Please visit bashscripts.org and usalug.org
#
#
########################################################

header ()
{
clear; echo "Bash Calculator - Enter a calculation and hit enter";
echo "---------------------------------------------------";
}

calc ()
{
header
while true
do read -p "" bashcalc;
        if [ "$bashcalc" = "quit" ]
          then
            exit
        fi

        if [ "$bashcalc" = "clear" ]
         then
           calc
        fi

        if [ "$bashcalc" = "help" ]
         then
           clear
           echo "Options include:"
           echo " help - This help file"
           echo " clear - Clears the screen"
           echo " quit - Quits the program"
           echo " "
           read -p  "Hit any key to continue" temp;
           calc
        fi

   echo "scale=4; ${bashcalc}" | bc ;
   echo "---------------------";
done
}

# Program run starts here
calc

exit


_________________

Zbook (Laptop)- Mint 18 64-bit - I7-4800mq - 32GB ram
Phantom (Desktop)- Mint 64-bit - i7-5770 - 32GB ram
Nas4Free (NAS) - 6X3TB File Server.
Sky (HP BladeCenter) - 16 blades - 536 GB ram-G7/G6/G5 blades.


Top
 Profile WWW  
 PostPosted: Sat Jan 21, 2012 10:37 am   
New Member

Joined: Sat Jan 21, 2012 10:22 am
Posts: 1
i took the first one and added to bashrc:

function calc {
echo "scale=4; $1" | bc
}

saves the need to add an extra file...


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


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:  
cron


Powered by phpBB © 2012 phpBB Group
© 2003 - 2012 USA LINUX USERS GROUP