LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 10-12-2011, 01:01 PM   #1
Ghazale
LQ Newbie
 
Registered: May 2011
Posts: 14

Rep: Reputation: Disabled
multiply two numbers in Linux script


Hi,

I have two numbers, num and ps that I want to multiply them, I tried different ways that I found from web, but none of them works. Could you please tell me how I multiply them is a script?

thanks a lot!
Ghazale
 
Old 10-12-2011, 01:11 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,620

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
Quote:
Originally Posted by Ghazale View Post
Hi,

I have two numbers, num and ps that I want to multiply them, I tried different ways that I found from web, but none of them works. Could you please tell me how I multiply them is a script?

thanks a lot!
Ghazale
Hi,

if they are both integers then you can do in bash:
Code:
#!/bin/bash
num=2
ps=3
ans=$((num*ps))
echo ${ans}
If they are floating point numbers then you can use bc:
Code:
#!/bin/bash
num=2.5
ps=3
ans=$(echo "${num}*${ps}" |bc)
echo ${ans}
 
Old 10-12-2011, 03:38 PM   #3
Ghazale
LQ Newbie
 
Registered: May 2011
Posts: 14

Original Poster
Rep: Reputation: Disabled
the code you gave me returns zero, I read num and ps from file as follows:

while read ps
do
echo $ps
done < pktSize

while read num
do
echo $num
done < alaki

how can I multiply num and ps?

thanks a lot!
 
Old 10-12-2011, 04:41 PM   #4
yancek
LQ Guru
 
Registered: Apr 2008
Distribution: PCLinux, Slackware
Posts: 7,337

Rep: Reputation: 1408Reputation: 1408Reputation: 1408Reputation: 1408Reputation: 1408Reputation: 1408Reputation: 1408Reputation: 1408Reputation: 1408Reputation: 1408
Do you want to run the script and get input for ps and num, multiply and get the result? My experience in programming of any type is very limited but out of curiosity I tried this and it got the expected output. I'm sure there are many and probably better ways of doing this.

Quote:
#!/bin/bash
while [ "$ps" != 0 ]
do
read -p "ps:" ps
read -p "num:" num
printf "Result: "
echo "$ps * $num" | bc
done
exit 0
Make it executable before running it.
 
Old 10-12-2011, 08:58 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,552

Rep: Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899
As usual, if you told the whole story first you would have received different information.

crts' post gives all the correct information for multiplying the given types of numbers.

Your extra information of how you are now reading the numbers from files is the issue.
A simple test for you:
Code:
#!/bin/bash

while read ps
do
    echo $ps
done < pktSize

echo $ps
On running the above you will see that the variable 'ps' is a local variable to the loop and hence has no value once the loop has concluded.

As an additional question, now that the extra information is available, do the files contain one number each or are there multiple numbers in each file?
If multiple, is it a one to one relationship between the numbers, ie. line 1 from each line to be multiplied together and so on, or do you require all numbers
from each file to be multiplied together, ie. line 1 in first file to multiply against all lines in file 2 and so on?

As you can see the issue has become much more complicated with only a small addition of information, therefore, if you have further information
which may impact the solution you should advise us now.
 
Old 10-12-2011, 10:06 PM   #6
bonixavier
Member
 
Registered: Sep 2010
Distribution: Slackware
Posts: 320

Rep: Reputation: 65
Why don't you use python? Much easier to do calculating in it than in bash.
 
  


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
Script to print range of numbers. First script raige Programming 2 11-15-2010 09:01 AM
[SOLVED] Script to add up numbers in textfile philipz Programming 3 05-20-2010 09:38 AM
LXer: Shell Script To Produce Prime Numbers On Linux And Unix LXer Syndicated Linux News 1 11-17-2009 03:54 PM
Multiply floats in bash script mkrems Linux - Software 5 02-04-2008 08:10 PM
LXer: Linux PC Models Multiply As Vista Struggles LXer Syndicated Linux News 0 01-15-2008 04:10 PM

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

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