[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

dividing numbers in unix

Posted on 2014-04-23
Medium Priority
316 Views
Hi
I am trying to divide two numbers , I tried various method but not getting result

I tried
p1= `\$(expr \$diffcount / \$basecount)`
p1= \$(expr \$diffcount / \$basecount)
p1= expr((\$diffcount / \$basecount))
p1= \$expr((\$diffcount / \$basecount))

diffcount and basecount values are generated by other expressions
I am getting values for those valuables . I am using Korn shell

thanks
0
Question by:uco
• 2
• 2

LVL 38

Expert Comment

ID: 40018642
You can try using bc instead of expr:

result=\$( echo "scale=3 ; \$diffcount / \$basecount" | bc )
echo \$result
0

Author Comment

ID: 40018698
Thanks Jansen , that works, one last one how do we multiply the result with another variable

thanks
0

LVL 38

Accepted Solution

Gerwin Jansen, EE MVE earned 2000 total points
ID: 40018720

\$diffcount * \$othervariable / \$basecount
0

Author Closing Comment

ID: 40018725
Thanks Jensen
0

LVL 20

Expert Comment

ID: 40018797
Also note that in all of your examples there was a space after the "=" sign - there must be no spaces either side of the "=" when assigning values in shell.

Without the space, the second expression would have been what you wanted (as long as you don't mind an integer result).
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month20 days, 4 hours left to enroll