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

Try:

sed 's/, /*/g' < abc | bc

are the number of digits fixed in file i.e. only 3 or can be more

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

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

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

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

Hi,
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:

/, /

to

/, */

instead.)

Hi,

I tried using all the commands.Its giving error as follows- sed: command garbled: s/, */g

sed 's/, */*/g' file.txt | bc

works fine for me.

That error indicates you've not typed it correctly, you've left out a /* between the * and / there.