Note that there are some explanatory texts on larger screens.

plurals
  1. POLinux Shell-Script And Recursion, Variable not retaining value
    primarykey
    data
    text
    <p>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.</p> <p>Here is the Code to the Non-Recursive Version :</p> <pre><code>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&lt;=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&lt;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" </code></pre> <p>Now, I went forward and implemented the same logic using the recursive approach.</p> <p>Here is the Recusive Implementation:</p> <pre><code>clear nonfib() { let c=$a+$b if [ $(( $c-$lastFib )) -gt 1 ] then for (( i=lastFib+1; i&lt;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" </code></pre> <p>But here, this line </p> <pre><code>if [ $(( $c-$lastFib )) -gt 1 ] </code></pre> <p>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.</p> <p>Need help with this.</p> <p><em>edit</em></p> <p>I ran the code like : <code>sh -x NonFibRecursive.sh 10</code>to check what was wrong.</p> <p>Here is the output :</p> <pre><code>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&lt;c )) + '[' 1 -gt 10 ']' + echo -ne '1 ' 1 + (( i++ )) + (( i&lt;c )) + a=1 + b=2 + lastfib=2 + '[' 0 -eq 1 ']' + nonfib + let c=1+2 + '[' 3 -gt 1 ']' + (( i=lastFib+1 )) + (( i&lt;c )) + '[' 1 -gt 10 ']' + echo -ne '1 ' 1 + (( i++ )) + (( i&lt;c )) + '[' 2 -gt 10 ']' + echo -ne '2 ' 2 + (( i++ )) + (( i&lt;c )) + a=2 + b=3 + lastfib=3 + '[' 0 -eq 1 ']' + nonfib + let c=2+3 + '[' 5 -gt 1 ']' + (( i=lastFib+1 )) + (( i&lt;c )) + '[' 1 -gt 10 ']' + echo -ne '1 ' 1 + (( i++ )) + (( i&lt;c )) + '[' 2 -gt 10 ']' + echo -ne '2 ' 2 + (( i++ )) + (( i&lt;c )) + '[' 3 -gt 10 ']' + echo -ne '3 ' 3 + (( i++ )) + (( i&lt;c )) + '[' 4 -gt 10 ']' + echo -ne '4 ' 4 + (( i++ )) + (( i&lt;c )) + a=3 + b=5 + lastfib=5 + '[' 0 -eq 1 ']' + nonfib + let c=3+5 + '[' 8 -gt 1 ']' + (( i=lastFib+1 )) + (( i&lt;c )) + '[' 1 -gt 10 ']' + echo -ne '1 ' 1 + (( i++ )) + (( i&lt;c )) + '[' 2 -gt 10 ']' + echo -ne '2 ' 2 + (( i++ )) + (( i&lt;c )) + '[' 3 -gt 10 ']' + echo -ne '3 ' 3 + (( i++ )) + (( i&lt;c )) + '[' 4 -gt 10 ']' + echo -ne '4 ' 4 + (( i++ )) + (( i&lt;c )) + '[' 5 -gt 10 ']' + echo -ne '5 ' 5 + (( i++ )) + (( i&lt;c )) + '[' 6 -gt 10 ']' + echo -ne '6 ' 6 + (( i++ )) + (( i&lt;c )) + '[' 7 -gt 10 ']' + echo -ne '7 ' 7 + (( i++ )) + (( i&lt;c )) + a=5 + b=8 + lastfib=8 + '[' 0 -eq 1 ']' + nonfib + let c=5+8 + '[' 13 -gt 1 ']' + (( i=lastFib+1 )) + (( i&lt;c )) + '[' 1 -gt 10 ']' + echo -ne '1 ' 1 + (( i++ )) + (( i&lt;c )) + '[' 2 -gt 10 ']' + echo -ne '2 ' 2 + (( i++ )) + (( i&lt;c )) + '[' 3 -gt 10 ']' + echo -ne '3 ' 3 + (( i++ )) + (( i&lt;c )) + '[' 4 -gt 10 ']' + echo -ne '4 ' 4 + (( i++ )) + (( i&lt;c )) + '[' 5 -gt 10 ']' + echo -ne '5 ' 5 + (( i++ )) + (( i&lt;c )) + '[' 6 -gt 10 ']' + echo -ne '6 ' 6 + (( i++ )) + (( i&lt;c )) + '[' 7 -gt 10 ']' + echo -ne '7 ' 7 + (( i++ )) + (( i&lt;c )) + '[' 8 -gt 10 ']' + echo -ne '8 ' 8 + (( i++ )) + (( i&lt;c )) + '[' 9 -gt 10 ']' + echo -ne '9 ' 9 + (( i++ )) + (( i&lt;c )) + '[' 10 -gt 10 ']' + echo -ne '10 ' 10 + (( i++ )) + (( i&lt;c )) + '[' 11 -gt 10 ']' + flag=1 + break + a=8 + b=13 + lastfib=13 + '[' 1 -eq 1 ']' + exit </code></pre> <p>This is where the problem starts from : let c=1+1 <code>'[' 2 -gt 1 ']'</code> it shows, 2 > 1. But how can there be 2 ? CurrentFib = 2 LastFib = 1</p> <p>So, 2-1 = 1 .</p> <p>I hope I explained the problem properly.. If further clarifications are required, please ask.</p> <p>Corrected Code :</p> <pre><code>clear nonfib() { let c=$a+$b if [ $(( $c-$lastFib )) -gt 1 ] then for (( i=lastFib+1; i&lt;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 </code></pre> <p>Thank You. Regards Priyabrata</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload