Welcome to the most active Linux Forum on the web.
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org How to write mathematical formula in Linux Shell Scripting
 Linux - Newbie This Linux forum is for members that are new to Linux. Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

 12-05-2013, 01:32 PM #1 linuxmantra Member   Registered: Dec 2013 Posts: 111 Rep: How to write mathematical formula in Linux Shell Scripting I am writing a code to calculate the profit and loss. For example, I want to write a equation as profit = (SP - CP)/ Cp or any other complex mathematical equation. How can I write these equation in linux shell script.
 12-05-2013, 01:43 PM #2 suicidaleggroll LQ Guru Contributing Member   Registered: Nov 2010 Location: Colorado Distribution: OpenSUSE, CentOS Posts: 5,508 Rep: send it to a program that can do math: Code: profit=\$(echo "(SP - CP) / Cp" | bc -l) Of course replacing SP, CP, and Cp with actual numbers or variables containing their values.
 12-05-2013, 02:30 PM #3 jdkaye LQ Guru   Registered: Dec 2008 Location: Westgate-on-Sea, Kent, UK Distribution: Debian Testing Amd64 Posts: 5,464 Rep: There are loads of tutorials around to help you learn to use arithmetic expressions in scripts. Here's one example. You can easily find many more. jdk
 12-05-2013, 02:36 PM #4 linuxmantra Member   Registered: Dec 2013 Posts: 111 Original Poster Rep: echo "Enter cost price" read cp echo "Enter selling price" read sp if [ \$cp -gt \$sp ] then echo "Loss percent" `expr ((\$cp - \$sp ) / \$cp) /*100`" else echo "Profit percent" `expr ((\$sp - \$cp ) / \$cp) /*100`" fi -- Need Help to degbug the code----
 12-05-2013, 04:25 PM #5 rknichols Senior Member   Registered: Aug 2009 Distribution: CentOS Posts: 3,469 Rep: Keep in mind that the shell and expr perform only integer arithmetic, so you either have to scale the input and format the output appropriately or else do the calculation in a program like bc that understands floating point.
 12-05-2013, 06:53 PM #6 grail LQ Guru   Registered: Sep 2009 Location: Perth Distribution: Manjaro Posts: 9,516 Rep: Was there a question in post #4? What help is it you need?

 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 Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post golmschenk Linux - Newbie 2 03-20-2012 09:39 PM josepjerry Programming 5 10-14-2011 08:26 AM tubu_cuteguy Linux - Newbie 5 07-28-2010 07:16 PM Micro420 Programming 14 05-19-2007 03:41 AM WinX187 Programming 4 04-21-2007 12:06 AM

LinuxQuestions.org

All times are GMT -5. The time now is 10:29 PM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -