Solved

dividing numbers in unix

Posted on 2014-04-23
5
260 Views
Last Modified: 2014-04-23
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
Comment
Question by:uco
  • 2
  • 2
5 Comments
 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 40018642
You can try using bc instead of expr:

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

Author Comment

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

thanks
0
 
LVL 37

Accepted Solution

by:
Gerwin Jansen earned 500 total points
ID: 40018720
Just add it here:

$diffcount * $othervariable / $basecount
0
 

Author Closing Comment

by:uco
ID: 40018725
Thanks Jensen
0
 
LVL 19

Expert Comment

by:simon3270
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
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.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now