BASH: Factorial using FOR loop not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH: Factorial using FOR loop not working
# 1  
Old 07-15-2010
BASH: Factorial using FOR loop not working

Hi,


I am trying to run the factorial script but it’s not working.

The code is mentioned below:
------------------------------------------------------------------
Code:
/home/gc> cat fact.sh
#!/bin/bash
# using one command line parameter
 
factorial=1
for (( number = 1; number <= $1 ; number++ ))
do
factorial=$[ $factorial * $number ]
done
echo The factorial of $1 is $factorial

------------------------------------------------------------------
Execution:
Code:
/home/gc> bash -x fact.sh 5
+ factorial=1
fact.sh: syntax error near unexpected token `(('
fact.sh: fact.sh: line 5: `for (( number = 1; number <= $1 ; number++ ))'

------------------------------------------------------------------

Can anyone please help?


Regards,
GC

Moderator's Comments:
Mod Comment Please use code tags for listings and program output.


---------- Post updated at 09:33 PM ---------- Previous update was at 06:32 PM ----------

Ok, I've got the reason; it's the bash shell version. I was running the script in an old bash version. I found a link where this was stated. It was mentioned that 'seq' command can be used instead but even that is not recognized on my bash.

I am using GNU bash, version 1.14.5(1)

Can anyone suggest some alternative?



Regards,
GC

Last edited by pludi; 07-15-2010 at 10:17 AM..
# 2  
Old 07-15-2010
You were really close. For arithmetic, use parentheses, not brackets:

Code:
(12:40:19\[D@DeCoBox15)
[~]$ cat fact
#!/bin/bash
# using one command line parameter

factorial=1
for (( number = 1; number <= $1 ; number++ ))
do
factorial=$(($factorial * $number))
done
echo The factorial of $1 is $factorial

(12:40:21\[D@DeCoBox15)
[~]$ bash fact 12
The factorial of 12 is 479001600

# 3  
Old 07-15-2010
seq is not a bash feature, but an external command. Apparently, your system does not have it. Perhaps you can try jot. Alternatively, you can use a while loop:
Code:
f=1 i=1
while [ $i -le $1 ]; do
    f=$((f*i))
    i=$((i+1))
done
echo $f

Regards,
Alister
# 4  
Old 07-17-2010
Thanks DeCoTwc & alister for your inputs!

@DeCoTwc: I tried your method but those braces also did not work. I got a similar error message.
Code:
/home/gc> bash unix1_fact.sh 5
 unix1_fact.sh: syntax error near unexpected token `(('
 unix1_fact.sh: unix1_fact.sh: line 5: `for (( number = 1; number <= $1 ; number++ ))'

Looks like the bash I am trying on is too old to understand even your suggested syntax. Smilie

I tried the same code on newer bash and it worked fine. Anyways, thanks for your input.

@alister: Your method was working fine. Thank You!

I also tried awk; it also works fine on my old bash version.


Regards,
GC
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For-loop not working

Hello all, I would like to unzip some files with a for-loop. Is there anyone who could tell me how I should do this - in a correct way? for file in $(ls); do echo gzip -d < $file | tar xf -; done The problem is the pipe - I believe. But how could I do it? I need it for the command... (4 Replies)
Discussion started by: API
4 Replies

2. Shell Programming and Scripting

README: Factorial quick chart with sed & bc:

Hi all, While doing some checks I found a kind of interesting arithmetic factorial chart with sed, sharing this may be simple but thought to share, # n=20;for i in `seq $n`;do printf "`seq $i|xargs|sed 's/ /*/g'`= ";echo "`seq $i|xargs|sed 's/ /*/g'`"| bc;done 1= 1 1*2= 2 1*2*3= 6... (6 Replies)
Discussion started by: rveri
6 Replies

3. Shell Programming and Scripting

Factorial of any number using functions

how to write the code for factorial of any number using functions and arguments????? (7 Replies)
Discussion started by: kullu
7 Replies

4. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

5. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

6. UNIX for Dummies Questions & Answers

Anyone know?: How the 'for'-loop could stop working in interactive bash shell?!

It is happening with my sessions already second time: a 'for'-loop for some reason stop to work as expected. That means or it is looping without exitting, or it is not loop even once. Here example of my try when it is not processing even one loop. You can see, I start new subshell and... (14 Replies)
Discussion started by: alex_5161
14 Replies

7. Shell Programming and Scripting

if loop not working in BASH shell

i have this code for a simple if loop: #!/bin/bash array="1 2 3 4 5" array2="5 6 7 8 9" if } -gt ${array} ]; then echo "${array2} is greater than ${array}!!" fi the error is ./script8: line 9: [: too many arguments ./script8: line 9: [: too many arguments ./script8: line 9: [:... (10 Replies)
Discussion started by: npatwardhan
10 Replies

8. Shell Programming and Scripting

bash and ksh: variable lost in loop in bash?

Hi, I use AIX (ksh) and Linux (bash) servers. I'm trying to do scripts to will run in both ksh and bash, and most of the time it works. But this time I don't get it in bash (I'm more familar in ksh). The goal of my script if to read a "config file" (like "ini" file), and make various report.... (2 Replies)
Discussion started by: estienne
2 Replies

9. UNIX for Advanced & Expert Users

If loop is not working

The following piece of code is not running because it is fails to go inside the if condition. i want to create a directory if there is no directory in the input path. i am using Linux, by CENT. Please help. echo " Enter the path where you u want to extract the tar" read EXTRACT_PATH ls -ld... (12 Replies)
Discussion started by: toanilsharma1
12 Replies

10. UNIX for Dummies Questions & Answers

If then else loop not working

If then else segment of below code is not working. For each filename code is displaying output for if part as well as else part. Please help its urgent. for usercusttop in `echo ${filename}|sort|uniq|cut -c 1-${actualwordcount}` do ... (2 Replies)
Discussion started by: findprakash
2 Replies
Login or Register to Ask a Question