LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 12-05-2013, 01:32 PM   #1
linuxmantra
Member
 
Registered: Dec 2013
Posts: 111

Rep: Reputation: Disabled
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.
 
Old 12-05-2013, 01:43 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,508

Rep: Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102
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.
 
Old 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: Reputation: Disabled
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
 
Old 12-05-2013, 02:36 PM   #4
linuxmantra
Member
 
Registered: Dec 2013
Posts: 111

Original Poster
Rep: Reputation: Disabled
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----
 
Old 12-05-2013, 04:25 PM   #5
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,469

Rep: Reputation: 1523Reputation: 1523Reputation: 1523Reputation: 1523Reputation: 1523Reputation: 1523Reputation: 1523Reputation: 1523Reputation: 1523Reputation: 1523Reputation: 1523
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.
 
Old 12-05-2013, 06:53 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,516

Rep: Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893
Was there a question in post #4? What help is it you need?
 
  


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
[SOLVED] Open/Libre Office Formula Editor - How to write reserved symbols? golmschenk Linux - Newbie 2 03-20-2012 09:39 PM
Shell scripting to write o/p of SELECT INTO OUTFILE into files of configurable size josepjerry Programming 5 10-14-2011 08:26 AM
a new shell for doing mathematical operation tubu_cuteguy Linux - Newbie 5 07-28-2010 07:16 PM
Shell scripting: How to write to multiple files? Micro420 Programming 14 05-19-2007 03:41 AM
First time using linux shell, loving it, can anyone help me write my own shell???? WinX187 Programming 4 04-21-2007 12:06 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:29 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration