program for multiplication in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting program for multiplication in shell script
# 1  
Old 08-12-2009
program for multiplication in shell script

Hi,
I wanted to write a schell program that fetches the values from a file and prints the output as its onerall multiplication.

for example I have a file named abc. it has values 2, 3, 4

now my program should give me 2*3*4 ie 24.

note:this file abc can have any numbers.

so experts,
please help me with this program.i am a newbie to unix schell programing Smilie
# 2  
Old 08-12-2009
Try:
Code:
sed 's/, /*/g' < abc | bc

# 3  
Old 08-12-2009
are the number of digits fixed in file i.e. only 3 or can be more
# 4  
Old 08-12-2009
hi..
It can be more...

---------- Post updated at 12:44 PM ---------- Previous update was at 12:39 PM ----------

hi jpriyank,

All I wanted is that i have a file say file1 now this file has some values say 2,5 ,6 ,7

now program should read this values from the file and give output as 420 (ie 2*5*6*7)

can you help me...plz
# 5  
Old 08-12-2009
try this script which i made keeping in mind u are new to shell programming

prod=1
#prod will store final product
i=`awk -F, '{ print NF}' abc`
#count number of digits into variable i
n=1
while [ $n -le $i ]
do
temp=`cut -d, -f$n abc`
#cutting each digit and storing in temp
prod=`expr $prod \* $temp`
n=`expr $n + 1`
done
echo $prod


i hope this helps
# 6  
Old 08-13-2009
woww thanks a lottttttt..... !! i ll try...it sure gave me many ideas..thanks again

---------- Post updated 08-13-09 at 11:07 AM ---------- Previous update was 08-12-09 at 01:18 PM ----------

Quote:
Originally Posted by jpriyank
try this script which i made keeping in mind u are new to shell programming

prod=1
#prod will store final product
i=`awk -F, '{ print NF}' abc`
#count number of digits into variable i
n=1
while [ $n -le $i ]
do
temp=`cut -d, -f$n abc`
#cutting each digit and storing in temp
prod=`expr $prod \* $temp`
n=`expr $n + 1`
done
echo $prod


i hope this helps
Hi,
Its not working.

---------- Post updated at 11:08 AM ---------- Previous update was at 11:07 AM ----------

Please help me with the solution. I have tried a lot but i am not able to.

---------- Post updated at 11:19 AM ---------- Previous update was at 11:08 AM ----------

Quote:
Originally Posted by Smiling Dragon
Try:
Code:
sed 's/, /*/g' < abc | bc

Hi,
This is not working.Can you please tell me how can i multiply the values inside a file.
# 7  
Old 08-13-2009
Quote:
Originally Posted by sandeep.krish
This is not working.Can you please tell me how can i multiply the values inside a file.
You may need to elaborate slightly on 'not working'...

Provided your file format is as you stated (numbers separated by a comma and space) it should work ok (tested fine on my server).

Post the error and we'll take a look

---------- Post updated at 06:42 PM ---------- Previous update was at 06:29 PM ----------

(BTW, if you aren't certain there will always be a space with each comma, change the first bit of the line from:
Code:
/, /

to
Code:
/, */

instead.)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Percentage / Multiplication in Shell Script

Hello, I am trying to compute the percentage in a script as shown below: PerCover=`echo "scale=2 ; 100 \* ($InputCover/$Total)" | bc` However the PerCover value is blank/null. What do I need to do differently? Thanks for your input! ~Guss (1 Reply)
Discussion started by: Gussifinknottle
1 Replies

2. Programming

Regarding Python Program with Shell Script

Hi All, I have written a shell script which is using the expect method, it is working fine in terminal window, and then I have executed via python script its also working fine in command prompt functioning properly, I used subprocess.Popen method to execute the shell script file, its working... (0 Replies)
Discussion started by: janaefx
0 Replies

3. Homework & Coursework Questions

Need help writing a shell script program

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write the a shell script program to remove all space characters stored in the shell variable TEXT.... (7 Replies)
Discussion started by: kofine05
7 Replies

4. Shell Programming and Scripting

Killing a program in shell script

I followed the directions here Free Twitter Source Code ? Twitter Database Server: Install and created a php script that enters twitter keyword searches into a MySQL DB. When you execute the files outlined in the above link, a script starts indefinitely. I've noticed that the scripts... (6 Replies)
Discussion started by: phpchick
6 Replies

5. Shell Programming and Scripting

shell script program

shell script in Unix/Linux to find the lines numbers of a text file are having word which is 5 to 10 characters long and having first letter as a capital letter. (3 Replies)
Discussion started by: usersnehal
3 Replies

6. Shell Programming and Scripting

Calling a shell script from a C program

Hi, I have a shell script which connects to a database and fetches the count of the records from a table. I want to embed this whole script in a C program. Also the count fetched should be available in the C program for further usage. Please let me know how this can be done. Thanks ... (0 Replies)
Discussion started by: swasid
0 Replies

7. Shell Programming and Scripting

Error with "multiplication table" in shell script

#!/bin/sh echo Enter the multiplication number required: read number for i in 1 2 3 4 5 6 7 8 9 10 do echo "$number * $i = expr $number \* $i" done I am not getting the output for this multiplication table. (4 Replies)
Discussion started by: vinodpaw
4 Replies

8. Shell Programming and Scripting

Need help on C-shell script program

#!/bin/csh # This program will add integers # # # add integer1 .. # # Check for argument if ($#argv == 0 ) then echo "usage: add integers" exit 1 else set input = $argv endif # set sum = 0 foreach var ( $input ) @sum = $sum + $input end # (1 Reply)
Discussion started by: haze21
1 Replies

9. Programming

How to include shell script in C program

hi I want to call a shell script in C program the script is : ssh -t user@remote sh /<remote>home/user/<file_name>.sh and other several commands C program : Call this script and the retrive the task that is been done in <file_name>.sh file can any one tell me how... (5 Replies)
Discussion started by: mridula
5 Replies

10. Filesystems, Disks and Memory

shell script program

shell script for sorting,searchingand insertion/deletion of elements in a list (1 Reply)
Discussion started by: jayaram_miryabb
1 Replies
Login or Register to Ask a Question