LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-16-2006, 12:18 PM   #1
xiekke
LQ Newbie
 
Registered: Feb 2006
Posts: 14

Rep: Reputation: 0
programming a calculator with bash


hi, i am trying to program a calculator,


**************************************************
#!/bin/bash

echo "(($1))"
***************************************************
.. and that is my whole program, works fine wth integers, but with decimals it would show error, can any help with calculating with decimals?
 
Old 03-16-2006, 12:59 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
echo "2.101592 + 1.04" | bc
 
Old 03-16-2006, 03:57 PM   #3
crouse
LQ Newbie
 
Registered: May 2005
Location: USA
Posts: 19

Rep: Reputation: 0
Code:
#!/bin/bash
# bash calculator
echo $1 | bc
exit
or really short
----------------------------
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:~>
The "scale=4" allows for division to be carried out to 4 decimal places. You can edit this to whatever length you wish to carry the division to.

Last edited by crouse; 03-16-2006 at 04:45 PM.
 
Old 03-18-2006, 08:49 PM   #4
xiekke
LQ Newbie
 
Registered: Feb 2006
Posts: 14

Original Poster
Rep: Reputation: 0
| bc, can you explain that part?
 
Old 03-18-2006, 11:10 PM   #5
crouse
LQ Newbie
 
Registered: May 2005
Location: USA
Posts: 19

Rep: Reputation: 0
| is the PIPE command . It runs the output of the previous command into the next command. In this case ... bc.

As for bc, direct from the man pages...

Quote:
bc is a language that supports arbitrary precision numbers with inter-
active execution of statements. There are some similarities in the
syntax to the C programming language. A standard math library is
available by command line option. If requested, the math library is
defined before processing any files. bc starts by processing code from
all the files listed on the command line in the order listed. After
all files have been processed, bc reads from the standard input. All
code is executed as it is read. (If a file contains a command to halt
the processor, bc will never read from the standard input.)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash programming winterhunter Linux - Newbie 6 11-26-2007 07:14 AM
bash programming alaios Programming 2 07-23-2005 12:52 AM
bash programming drisay Programming 6 12-20-2004 09:09 AM
bash programming cpukiller Linux - Newbie 1 11-04-2003 06:45 PM
bash programming bulliver Programming 2 04-05-2003 09:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:34 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