LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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: 113

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,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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,465

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: 113

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: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
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: 10,007

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


Reply



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 11:00 AM.

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
Open Source Consulting | Domain Registration