Which is the best way/command to do mathematics in UNIX scripting?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Which is the best way/command to do mathematics in UNIX scripting?
# 1  
Old 03-15-2010
Question Which is the best way/command to do mathematics in UNIX scripting?

Hello all!
I used to use expr for doing simple mathematics, but has a main advantage and a main disadvantage:
The advantage is that it can take variables for numbers
Code:
(e.g.{1}: echo "Give me first"
read lol
echo "Give other"
read lil
sum=`expr $lol + $lil`
echo "The sum of $lol and $lil = $sum")

The disadvantage is that it cannot give float numbers.It gives errors (example for 2 / 5 .......)
Somebody told me about bc but I cannot give him variables.The program I wanna make is similar to example {1} but I need float numbers!
if the program is like this:
Code:
read lol
read lil
echo "scale=2; $lol - $lil" | bc

bc gives error.The only way to avoid this error is this way:
Code:
lol=5 ; lil=2 ; echo "scale=2; $lol - $lil" | bc

But in this way you don't allow the user to choose himself the variables.
Somebody told me that
Code:
read lol
read lil
echo "scale=2; $lol - $lil" | bc

works, but unfortunately in my OS doesn't work(Ubuntu 9.10)
Any suggestions/ideas????
# 2  
Old 03-15-2010
# 3  
Old 03-15-2010
# 4  
Old 03-15-2010
Use "bc -l" for floating point result, but for actually retaining proper scale you would need to use printf.
# 5  
Old 03-15-2010
Quote:
read lol
read lil
echo "scale=2; $lol - $lil" | bc

A hunch. How did you execute these three lines?

If you type them one-by-one at the command prompt answering each "read" before typing the next command ... they should work.

If you put them in a shell script and execute the script ... they should work.

If you cut/paste all three lines onto the command line they will fail. This is because the second line becomes the input to the first line meaning that $lol contains the string "read lil" which in turn makes the echo contain rubbish to pass to "bc".
# 6  
Old 03-15-2010
Thx for your replies.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. What is on Your Mind?

Forum Trivial Pursuit - New Computer Science and Mathematics Trivia for UNIX.com

I have added a new experimental "Computer Science and Mathematics Trivia - True or False" section in the discussions, currently under the tags box. In the future, I plan to Expand this feature to add more trivia categories from math, science and technology. Keep track of correct and... (20 Replies)
Discussion started by: Neo
20 Replies

2. Shell Programming and Scripting

If else condition inside for loop of awk command in UNIX shell scripting

Hi , Please excuse me for opening a new thread i am unable to find out the syntax error in my if else condition inside for loop in awk command , my actual aim is to print formatted html td tag when if condition (True) having string as "failed", could anyone please advise what is the right... (2 Replies)
Discussion started by: karthikram
2 Replies

3. UNIX for Dummies Questions & Answers

Unix Shell Scripting( Calling from Unix to PLSQL)

Hello Experts, I have the following questions to be discussed here at this esteemed discussion forum. I have two Excel sheets which contain Unix Commands llike creating directory the structure/ftp/Copy/Zip etc to basically create an environment. I need help in understanding some of... (1 Reply)
Discussion started by: faizsaadq
1 Replies

4. Shell Programming and Scripting

recommend book for unix command/ shell scripting

Hi, i wish to learn unix commands and shell scripting. platform is solaris. but i am focused more on handy unix commands than system administration. which books do you recommend? (1 Reply)
Discussion started by: nurulamin862
1 Replies

5. Shell Programming and Scripting

hell & mathematics

I've been able to generate output based on the code scarfake provided me (thanks again man). A little background so everyone more or less knows whats going on: I needed code that would propagate a database with 100,000 entries, for capacity testing purposes, something like a stress test. ... (5 Replies)
Discussion started by: ogoy
5 Replies

6. Shell Programming and Scripting

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies

7. UNIX for Dummies Questions & Answers

mathematics in shell script

Hi Friends, I am new to shell scripting and a trying to work this out. I have a variable called $number. The value in this variable is 10 I want to display $number - 2 in a mail. The result would be 8 eg. mail -s 'subject' user <<eof the result of your calculation is $number - 2... (2 Replies)
Discussion started by: sureshy
2 Replies

8. UNIX for Dummies Questions & Answers

mathematics operations in unix

Hello guys! Can say me anybody about operatios with unix, I don't to make operations, only inside in a variable, like this #y=4 #x=2 #let z=$y-$x #echo $z # 2 but I can't to make mathematical operations with decimal like this #y=3.2 #x=1.5 #let z=$y-$x #echo $z # 3 this... (2 Replies)
Discussion started by: cesar720213
2 Replies
Login or Register to Ask a Question