Register
It is currently Mon Oct 20, 2014 7:00 am

Quick bash calculator.


All times are UTC - 6 hours


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

Joined: Sun May 15, 2005 9:36 pm
Posts: 669
Location: Des Moines, 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:
crouse@linux:~> calc 3.555+7.999
11.554
crouse@linux:~>


Note: scale=4 allows for division to be carried out 4 decimal places, you can change that to whatever you wish ;)


Top
 Profile WWW  
 PostPosted: Sun Mar 19, 2006 2:00 pm   

Joined: Fri Oct 14, 2005 9:30 am
Posts: 61
Instead of putting as an alias in your .bashrc file, couldn't you just put it in a folder that is in your path (eg /usr/bin)


Top
 Profile WWW  
 PostPosted: Sun Mar 19, 2006 10:57 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 669
Location: Des Moines, Iowa
sure you can..... as long as you have root access ;)

If you don't, my way STILL works, and keeps your script with the rest of your scripts ;)


Top
 Profile WWW  
 PostPosted: Mon Mar 20, 2006 10:02 am   

Joined: Fri Oct 14, 2005 9:30 am
Posts: 61
You only need root access to run it if you put in /sbin/ but you do need root access to put it in /usr/bin - just not to run it. I put my scripts in /usr/bin or in a central folder that I add to my system path.


Top
 Profile WWW  
 PostPosted: Tue Mar 21, 2006 9:31 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 669
Location: Des Moines, Iowa
crudball wrote:
You only need root access to run it if you put in /sbin/ but you do need root access to put it in /usr/bin - just not to run it. I put my scripts in /usr/bin or in a central folder that I add to my system path.


The important words in your statement but you do need root access to put it in /usr/bin

Like I just said..... if you DONT have root access..... you CAN'T put it in /usr/bin ..... hence why I suggested making an alias in .bashrc

If you DO have root access..... putting in in /usr/bin is fine IF you want everyone else to be able to run the script as well....... if you don't, then WHY would you want to ? Aliased in .bashrc and held YOUR /home/$USER/Scripts directory makes more sense to me, but I guess that's just a personal preference. I like all of my scripts in one place. Easier to backup and remember where they are.


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 11 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