Priyabrata
Priyabrata

Reputation: 1232

Linux Shell-Script And Recursion, Variable not retaining value

I am writing a simple Non-Fibonacci Sequence generator, with the range taken in using command line argument. I've already implemented the Non_Recursive version, and it works as expected.

Here is the Code to the Non-Recursive Version :

clear
if [ $# -ne 1 ]; then
    echo "Invalid Argument."
    exit
fi

max=$1

a=0
b=1
echo "Non-Fibonacci Numbers :"
lastFib=0
flag=0
for (( i=1; i<=i+1; i++ ))
do
    if [ $flag -eq 1 ]
    then
    break
    fi
    let c=$a+$b
    if [ $(( $c-$lastFib )) -gt 1 ]
    then
    for (( j=lastFib+1; j<c; j++ ))
    do
        if [ $j -gt $max ]
        then
        flag=1
        break
        fi
        echo -ne $j",  "
    done
    fi
    a=$b
    b=$c
    let lastFib=$c 
done
echo -e "\n"

Now, I went forward and implemented the same logic using the recursive approach.

Here is the Recusive Implementation:

clear

nonfib()
{
    let c=$a+$b
    if [ $(( $c-$lastFib )) -gt 1 ]
    then
    for (( i=lastFib+1; i<c; i++ ))
    do
        if [ $i -gt $limit ]
        then
        flag=1
        break
        fi
        echo -ne $i" "
    done
    fi
    a=$b
    b=$c
    lastfib=$c
    if [ $flag -eq 1 ]
    then
    exit
    fi
    nonfib
}

if [ $# -ne 1 ]
then
    echo "Invalid Argument!!"
    exit
fi
a=0
b=1
flag=0
lastFib=0
c=0
limit=$1
echo "Non-Fibonacci Numbers : "
nonfib
echo -e"\n"

But here, this line

if [ $(( $c-$lastFib )) -gt 1 ] 

fails to do what I expect it to do. It checks the difference between the current fibonacci number and the last generated number to see, if there are values in between them. But this is where, the execution is going wrong.

Need help with this.

edit

I ran the code like : sh -x NonFibRecursive.sh 10to check what was wrong.

Here is the output :

Non-Fibonacci Numbers : 
1 1 2 1 2 3 4 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 9 10 [babi@localhost ShellScripts]$ sh -x NonFibRecursive.sh 10
+ clear
+ '[' 1 -ne 1 ']'
+ a=0
+ b=1
+ flag=0
+ lastFib=0
+ c=0
+ limit=10
+ echo 'Non-Fibonacci Numbers : '
Non-Fibonacci Numbers : 
+ nonfib
+ let c=0+1
+ '[' 1 -gt 1 ']'
+ a=1
+ b=1
+ lastfib=1
+ '[' 0 -eq 1 ']'
+ nonfib
+ let c=1+1
+ '[' 2 -gt 1 ']'
+ (( i=lastFib+1 ))
+ (( i<c ))
+ '[' 1 -gt 10 ']'
+ echo -ne '1 '
1 + (( i++  ))
+ (( i<c ))
+ a=1
+ b=2
+ lastfib=2
+ '[' 0 -eq 1 ']'
+ nonfib
+ let c=1+2
+ '[' 3 -gt 1 ']'
+ (( i=lastFib+1 ))
+ (( i<c ))
+ '[' 1 -gt 10 ']'
+ echo -ne '1 '
1 + (( i++  ))
+ (( i<c ))
+ '[' 2 -gt 10 ']'
+ echo -ne '2 '
2 + (( i++  ))
+ (( i<c ))
+ a=2
+ b=3
+ lastfib=3
+ '[' 0 -eq 1 ']'
+ nonfib
+ let c=2+3
+ '[' 5 -gt 1 ']'
+ (( i=lastFib+1 ))
+ (( i<c ))
+ '[' 1 -gt 10 ']'
+ echo -ne '1 '
1 + (( i++  ))
+ (( i<c ))
+ '[' 2 -gt 10 ']'
+ echo -ne '2 '
2 + (( i++  ))
+ (( i<c ))
+ '[' 3 -gt 10 ']'
+ echo -ne '3 '
3 + (( i++  ))
+ (( i<c ))
+ '[' 4 -gt 10 ']'
+ echo -ne '4 '
4 + (( i++  ))
+ (( i<c ))
+ a=3
+ b=5
+ lastfib=5
+ '[' 0 -eq 1 ']'
+ nonfib
+ let c=3+5
+ '[' 8 -gt 1 ']'
+ (( i=lastFib+1 ))
+ (( i<c ))
+ '[' 1 -gt 10 ']'
+ echo -ne '1 '
1 + (( i++  ))
+ (( i<c ))
+ '[' 2 -gt 10 ']'
+ echo -ne '2 '
2 + (( i++  ))
+ (( i<c ))
+ '[' 3 -gt 10 ']'
+ echo -ne '3 '
3 + (( i++  ))
+ (( i<c ))
+ '[' 4 -gt 10 ']'
+ echo -ne '4 '
4 + (( i++  ))
+ (( i<c ))
+ '[' 5 -gt 10 ']'
+ echo -ne '5 '
5 + (( i++  ))
+ (( i<c ))
+ '[' 6 -gt 10 ']'
+ echo -ne '6 '
6 + (( i++  ))
+ (( i<c ))
+ '[' 7 -gt 10 ']'
+ echo -ne '7 '
7 + (( i++  ))
+ (( i<c ))
+ a=5
+ b=8
+ lastfib=8
+ '[' 0 -eq 1 ']'
+ nonfib
+ let c=5+8
+ '[' 13 -gt 1 ']'
+ (( i=lastFib+1 ))
+ (( i<c ))
+ '[' 1 -gt 10 ']'
+ echo -ne '1 '
1 + (( i++  ))
+ (( i<c ))
+ '[' 2 -gt 10 ']'
+ echo -ne '2 '
2 + (( i++  ))
+ (( i<c ))
+ '[' 3 -gt 10 ']'
+ echo -ne '3 '
3 + (( i++  ))
+ (( i<c ))
+ '[' 4 -gt 10 ']'
+ echo -ne '4 '
4 + (( i++  ))
+ (( i<c ))
+ '[' 5 -gt 10 ']'
+ echo -ne '5 '
5 + (( i++  ))
+ (( i<c ))
+ '[' 6 -gt 10 ']'
+ echo -ne '6 '
6 + (( i++  ))
+ (( i<c ))
+ '[' 7 -gt 10 ']'
+ echo -ne '7 '
7 + (( i++  ))
+ (( i<c ))
+ '[' 8 -gt 10 ']'
+ echo -ne '8 '
8 + (( i++  ))
+ (( i<c ))
+ '[' 9 -gt 10 ']'
+ echo -ne '9 '
9 + (( i++  ))
+ (( i<c ))
+ '[' 10 -gt 10 ']'
+ echo -ne '10 '
10 + (( i++  ))
+ (( i<c ))
+ '[' 11 -gt 10 ']'
+ flag=1
+ break
+ a=8
+ b=13
+ lastfib=13
+ '[' 1 -eq 1 ']'
+ exit

This is where the problem starts from : let c=1+1 '[' 2 -gt 1 ']' it shows, 2 > 1. But how can there be 2 ? CurrentFib = 2 LastFib = 1

So, 2-1 = 1 .

I hope I explained the problem properly.. If further clarifications are required, please ask.

Corrected Code :

clear

nonfib()
{
    let c=$a+$b
    if [ $(( $c-$lastFib )) -gt 1 ]
    then
    for (( i=lastFib+1; i<c; i++ ))
    do
        if [ $i -gt $limit ]
        then
        flag=1
        break
        fi
        echo -ne $i" "
    done
    fi
    a=$b
    b=$c
    lastFib=$c
    if [ $flag -eq 1 ]
    then
    echo
    exit
    fi
    nonfib
}

if [ $# -ne 1 ]
then
    echo "Invalid Argument!!"
    exit
fi
a=0
b=1
flag=0
lastFib=0
c=0
limit=$1
echo "Non-Fibonacci Numbers : "
nonfib

Thank You. Regards Priyabrata

Upvotes: 2

Views: 617

Answers (1)

user000001
user000001

Reputation: 33327

The problem is this line:

lastfib=$c

It should be:

lastFib=$c
#   ^
# Capital F

Variable names in bash are case-sensitive.

Upvotes: 2

Related Questions