# program for multiplication in shell script

**URL:** <https://community.unix.com/t/program-for-multiplication-in-shell-script/246841>\
**Category:** Shell Programming and Scripting\
**Created:** [August 12, 2009, 2:54am UTC](https://community.unix.com/t/program-for-multiplication-in-shell-script/246841 "2009-08-12T02:54:43Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![sandeep.krish](https://community.unix.com/letter_avatar/sandeep.krish/32/6_449c533f586939b6e5f246632a46421f.png) [@sandeep.krish](https://community.unix.com/u/sandeep.krish)\
**Post date:** [August 12, 2009, 2:54am UTC](https://community.unix.com/t/program-for-multiplication-in-shell-script/246841/1 "2009-08-12T02:54:43Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![Smiling\_Dragon](https://community.unix.com/user_avatar/community.unix.com/smiling_dragon/32/722_2.png) [@Smiling\_Dragon](https://community.unix.com/u/Smiling_Dragon)\
**Post date:** [August 12, 2009, 3:07am UTC](https://community.unix.com/t/program-for-multiplication-in-shell-script/246841/2 "2009-08-12T03:07:30Z")

</div>

Try:

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

```

---

<div class="post-metadata">

**Author:** ![jpriyank](https://community.unix.com/letter_avatar/jpriyank/32/6_449c533f586939b6e5f246632a46421f.png) [@jpriyank](https://community.unix.com/u/jpriyank)\
**Post date:** [August 12, 2009, 3:07am UTC](https://community.unix.com/t/program-for-multiplication-in-shell-script/246841/3 "2009-08-12T03:07:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sandeep.krish](https://community.unix.com/letter_avatar/sandeep.krish/32/6_449c533f586939b6e5f246632a46421f.png) [@sandeep.krish](https://community.unix.com/u/sandeep.krish)\
**Post date:** [August 12, 2009, 3:14am UTC](https://community.unix.com/t/program-for-multiplication-in-shell-script/246841/4 "2009-08-12T03:14:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jpriyank](https://community.unix.com/letter_avatar/jpriyank/32/6_449c533f586939b6e5f246632a46421f.png) [@jpriyank](https://community.unix.com/u/jpriyank)\
**Post date:** [August 12, 2009, 3:40am UTC](https://community.unix.com/t/program-for-multiplication-in-shell-script/246841/5 "2009-08-12T03:40:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sandeep.krish](https://community.unix.com/letter_avatar/sandeep.krish/32/6_449c533f586939b6e5f246632a46421f.png) [@sandeep.krish](https://community.unix.com/u/sandeep.krish)\
**Post date:** [August 13, 2009, 1:49am UTC](https://community.unix.com/t/program-for-multiplication-in-shell-script/246841/6 "2009-08-13T01:49:32Z")

</div>

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

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

> [@smiling dragon](#):
>
> Try:
> 
> ```plaintext
> sed 's/, /*/g' < abc | bc
> 
> ```

Hi,  
This is not working.Can you please tell me how can i multiply the values inside a file.

---

<div class="post-metadata">

**Author:** ![Smiling\_Dragon](https://community.unix.com/user_avatar/community.unix.com/smiling_dragon/32/722_2.png) [@Smiling\_Dragon](https://community.unix.com/u/Smiling_Dragon)\
**Post date:** [August 13, 2009, 2:42am UTC](https://community.unix.com/t/program-for-multiplication-in-shell-script/246841/7 "2009-08-13T02:42:11Z")

</div>

> [@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:

```plaintext
/, /

```

to

```plaintext
/, */

```

instead.)

---

<div class="post-metadata">

**Author:** ![sandeep.krish](https://community.unix.com/letter_avatar/sandeep.krish/32/6_449c533f586939b6e5f246632a46421f.png) [@sandeep.krish](https://community.unix.com/u/sandeep.krish)\
**Post date:** [August 13, 2009, 4:37am UTC](https://community.unix.com/t/program-for-multiplication-in-shell-script/246841/8 "2009-08-13T04:37:17Z")

</div>

> [@smiling dragon](#):
>
> 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:
> 
> ```plaintext
> /, /
> 
> ```
> 
> to
> 
> ```plaintext
> /, */
> 
> ```
> 
> instead.)

Hi,

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

---

<div class="post-metadata">

**Author:** ![panyam](https://community.unix.com/letter_avatar/panyam/32/6_449c533f586939b6e5f246632a46421f.png) [@panyam](https://community.unix.com/u/panyam)\
**Post date:** [August 13, 2009, 9:21am UTC](https://community.unix.com/t/program-for-multiplication-in-shell-script/246841/9 "2009-08-13T09:21:26Z")

</div>

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

```

works fine for me.

---

<div class="post-metadata">

**Author:** ![Smiling\_Dragon](https://community.unix.com/user_avatar/community.unix.com/smiling_dragon/32/722_2.png) [@Smiling\_Dragon](https://community.unix.com/u/Smiling_Dragon)\
**Post date:** [August 16, 2009, 8:21pm UTC](https://community.unix.com/t/program-for-multiplication-in-shell-script/246841/10 "2009-08-16T20:21:43Z")

</div>

> [@sandeep.krish](#):
>
> sed: command garbled: s/, \*/g

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