All I want to do is Multiply...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting All I want to do is Multiply...
# 1  
Old 02-28-2010
All I want to do is Multiply...

All I want to do is find 5!.

Code:
read num
num={1..5}  
while [ "$num" -le 5 ]
do
f=[ "$num" * "$num" ]
done
echo f

Error Msg.
Code:
line 5: ${1..5} bad substitution
line 6: integer expression expected

Line 5 is the num=...
Line 6 is the "while" statement

I am new at this, and I am really, really trying.
Please helpSmilie

Last edited by Yogesh Sawant; 03-01-2010 at 01:35 PM.. Reason: added code tags
# 2  
Old 02-28-2010
Something like this?
Code:
f=1

for num in {1..5}
do
  f=$(( $f * $num ))
done

echo $f

# 3  
Old 02-28-2010
All I want to do is Multiply...

Thank you so much Franklin52.
I got my expected results after numerous syntax errors. :-)
I had
Code:
do f=$(( "$f" * "$num" ))
done
echo "$f"

Bash did not like all of my quotes. I thought I had to use double quotes to reference variables.

This was the syntax error:
Code:
line 8: "1" * "1": syntax error: operand expected (error token is " "1" *  "1" ")

Any idea why sh did not like my quotes.

I got rid of the quotes an the script works fine.Smilie
Ccccc
Thanks again.

Last edited by Yogesh Sawant; 03-01-2010 at 01:34 PM.. Reason: added code tags
# 4  
Old 03-01-2010
Dear Friend,

You can use the following coding also.

Code:
i=1

for fact in `seq 1 5`
do
          i=$(( $i * $fact ))
done

  echo $i

# 5  
Old 03-01-2010
Using a Perl script you can achieve the requirement as follows

Code:
my $i = 1;
my $result = 1;
$result *= $i++ while ($i <= 5) ;
print "Value : $result\n";

# 6  
Old 03-01-2010
Franklin52 code is working fine.If it isn't working to you(Ccccc),then try the following code.

Code:
f=1

for fact in {1..5}
do
      let f*=fact
done

echo "Factorial of 5 is:$f"


Last edited by vivekraj; 03-01-2010 at 04:43 AM..
# 7  
Old 03-01-2010
Many, many thanks to Franklin52, Murugaperumal, thillai_selvan, & Vivekraj. Franklin52's code is working great for me, and the other code is enlarging my shell knowledge base.

Does anyone know how I can code this for any number, x!?

Ex. "please enter a number: "
read some number

I don't know how to code the "some number" multiplication part.
Thank again,
Ccccc
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Multiply values from a file

Hi All, I extracted a file using an awk command like this awk '{print substr($0,78,5)"," substr($0,59,6) "," substr($0,81,3) "," substr($0,11,7)}' file1 >> OUTPUT cat OUTPUT 00001,100000,005,0000080 00001,100000,008,0000220 00001,100000,001,0001000 00010,100000,001,0000400 I want... (3 Replies)
Discussion started by: arunkumar_mca
3 Replies

2. UNIX for Dummies Questions & Answers

Multiply value by row

Hi I would like to know how can I multiply the value of column three to columns 4-end of file example of input file: rs1000012 AK8 2 0.05 0.05 1 0 0 rs10000154 PAQR3 0.01 2 1 2 2 1 Desired output file: rs1000012 AK8 ... (1 Reply)
Discussion started by: fadista
1 Replies

3. Shell Programming and Scripting

Multiply tags in xml

Please advice best way to do this. prefered awk or sed input file : <?xml version='1.0' encoding='ISO-8859-1'?> <myxml> <UID></UID> <Dealer>0002</Dealer> <Range> <FromMonths>24</FromMonths> <ToMonths>35</ToMonths> </Range> </myxml> desired output : ]<?xml version='1.0'... (3 Replies)
Discussion started by: theshashi
3 Replies

4. Shell Programming and Scripting

multiply with awk

HI help i have cc 9+37.50 328611.50 688498.25 42.38 cc 66+62.50 328636.50 688498.42 42.58 i want to make o/p cc 9+3750 328611.50 688498.25 42.38 cc 66+6250 328636.50 688498.42 42.58 plz help (2 Replies)
Discussion started by: Indra2011
2 Replies

5. UNIX for Dummies Questions & Answers

Multiply two variables in t shell

Hello, How can I multiply two variables that contain floating point numbers. For example, how can I get the value for c a=2.165 b=3.234 c=a+3*b Thanks, Guss (1 Reply)
Discussion started by: Gussifinknottle
1 Replies

6. UNIX for Dummies Questions & Answers

Finding multiply directories

I have multiply directories scattered throughout my system that end in 2011. Example: one_2011 two_2011 three_2011 etc.... I'm trying to find all of these directories but coming up short. I tried find / -type d -name *2011 > example Any suggestions? I already searched in the... (13 Replies)
Discussion started by: shorty
13 Replies

7. Shell Programming and Scripting

Multiply numbers from different files

Hi All, I have tried few things with this but it did not help much. I have some 200,000 files in a directory. There are two sets of files. 1. Files with extension .dat with file names like these (1.dat, 2.dat, 5.dat, 8.dat....200000.dat) 2. Another set of files with .txt extension and... (5 Replies)
Discussion started by: shoaibjameel123
5 Replies

8. Shell Programming and Scripting

Multiply whole column with a value

Hi, I need to multiply 3rd column (comma seperated) of entire file by a value say 2.2. Suppose the file is: C,Gas $ YTD(TRI),15512.36,01/01/2010 New file should be (3rd column value multiplied by 2.2): C,Gas $ YTD(TRI),34127.192,01/01/2010 (5 Replies)
Discussion started by: yale_work
5 Replies

9. Shell Programming and Scripting

Multiply variables

I have no idea about programming, just know some simple html :confused:and I need to get to somebody that can help me with creating the application (?) that will multiply 2 varibales and given price (height x lenght) x $$$. PLEASE HELP!:confused: edit by bakunin: Anita, as much as we... (2 Replies)
Discussion started by: Anita Flejter
2 Replies

10. Shell Programming and Scripting

multiply variable

I need to multiply the value of a variable and then store it in another variable. I have EXPHOURINSEC=$(($EXPDATEHOUR * 3600)) but i get an error saying the * is unexpected. Im using ksh (4 Replies)
Discussion started by: fwabbly
4 Replies
Login or Register to Ask a Question