Fibonacci series -going into infinite loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Fibonacci series -going into infinite loop
# 1  
Old 12-09-2015
Linux Fibonacci series -going into infinite loop

Hello, I am a beginner to shell programming. Coded the following for Fibonacci series.

Code:
#!/bin/bash
fib()  {

i=0
j=1
arr[0]=0
arr[1]=1

echo "enter the limit:"
read n

while [ $i -lt $n ]
do
    fo= expr $j - 1
    f1=$j
    f2= expr $j + 1
    arr[$f2]= expr ${arr[$f1]} + ${arr[$f0]}
    echo ${arr[$f2]}
    
    i= expr $i + 1
    j= expr $j + 1
    
done
}

fib



The program is going into infinite loop. From debugging I understood that updated i,j values are not being carried to next iteration of while loop. Why is it so? Can someone please help.

Last edited by Don Cragun; 12-09-2015 at 02:55 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 12-09-2015
Hi, you must use backquote when you execute commande from variable affectation:
Code:
i=`expr $i + 1`

or
Code:
i=$(expr $i + 1)

or under bash, you needn't use expr, just:
Code:
((i++))

# 3  
Old 12-09-2015
Please use code tags as required by forum rules!

You need to use command substitution for all your expr assignments, e.g.
Code:
i=$(expr $i + 1)

# 4  
Old 12-09-2015
Thank you disedorgue & RudiC. The changes worked. ;-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Infinite loop query

I have a script script.shwhich is scheduled to run at 11 AM everyday. # script.sh Code: ./scb_script.sh & unfortunately scb_script.sh is running today in infinite loop as respective files are not available. My question, when script.sh starts running tomorrow, will the old process be... (1 Reply)
Discussion started by: JSKOBS
1 Replies

2. Shell Programming and Scripting

My for loop decides to become an infinite loop?

Hi, I was debating if I should put this in the dummies or scripts section, I apologize in advance if I chose poorly. Fairly new to Unix and BASH scripting but I thought I made it fairly well given my limited understanding. However, the output indicates that it's looping and I'm ending up with a... (5 Replies)
Discussion started by: gotreef
5 Replies

3. UNIX for Advanced & Expert Users

Procmail and infinite loop

I wanted to copy (not forward but copy) all incoming email to another address of mine. It worked, but now I encountered an infinite loop problem: When the second address doesn't like the content and bounces the message back, the bounce message will be sent back and forth. So, what I have in... (1 Reply)
Discussion started by: distill
1 Replies

4. Homework & Coursework Questions

Help with shell script to find sum of first n numbers of Fibonacci series

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: Shell script to find sum of first n numbers of Fibonacci series 2. Relevant commands, code, scripts,... (0 Replies)
Discussion started by: Kshitija
0 Replies

5. Shell Programming and Scripting

Shell script to generate Fibonacci series using recursion

I am facing problem with Shell script to generate Fibonacci series using recursion i.e. recursive function. Here is my script: #!/bin/sh fibo() { no=$1 if ; then return 0 elif ; then return 1 else a1=`expr $no - 1` fibo $a1 ... (10 Replies)
Discussion started by: Tapas Bose
10 Replies

6. Shell Programming and Scripting

Infinite while loop

what is the difference between while:,while true and while false? (6 Replies)
Discussion started by: proactiveaditya
6 Replies

7. Shell Programming and Scripting

infinite while do loop problem

hi all, this is how my scrip looks like #!/bin/sh bindir='/opt/apps/script/bin' datadir='/opt/apps/script/data' dir='/opt/apps/script' while : ; do ls -1rt /opt/apps/script/data/check.txt*|tail -1 > /dev/null 2>&1 if ;then chmod +rwx $bindir/dummy2.sh ... (8 Replies)
Discussion started by: tententen
8 Replies

8. Shell Programming and Scripting

Infinite loop not looping

Hi guys, I'm having a problem getting my infinite loop to loop. It simply reads in the users choice form the menu, executes the corresponding case statement and quits instead of looping back to the main menu again. I have a feeling it might be something with my if then statements within the case... (2 Replies)
Discussion started by: hootdocta5
2 Replies

9. Shell Programming and Scripting

problem in fibonacci series

hi, I'm a beginner to UNIX and got some problem in this fibonacci.Please help me out.Here is the code: fibo() { if then fibo=` expr {fibo ($1 - 2)} + {fibo ($1 - 1)}` | bc echo $fibo fi } echo "enter a number:" read x #echo "The fibonnacci series for value $x is:" fibo $x ... (4 Replies)
Discussion started by: janani_kalyan
4 Replies

10. Shell Programming and Scripting

Fibonacci series

Need code to run the Fibonacci series from 0 to 10 (16 Replies)
Discussion started by: nycol
16 Replies
Login or Register to Ask a Question