What am I doing wrong here (issue with loops)

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite
1












This thing here



n=0
x=1
while [ $n -lt 6 ]
do
n=$(( n+1 ))
echo "sasadgsad gsda $n" >> /home/test/rptest

if [ $n -eq 5 ]
then
while [ $x -le 5 ]
do
echo "end of line$x" >> /home/test/rptest
x=$(( x+1 ))
done
fi
done


Output this thing



sasadgsad gsda 1
sasadgsad gsda 2
sasadgsad gsda 3
sasadgsad gsda 4
sasadgsad gsda 5
end of line1
end of line2
end of line3
end of line4
end of line5
sasadgsad gsda 6


That 11th line shouldn't be there... Shouldn't the first while thing finish when n = 5? Why does it create that last line?



Thank you for the help :)



I even tried it with n=1 and while [ $n -le 5 ]










share|improve this question



























    up vote
    0
    down vote

    favorite
    1












    This thing here



    n=0
    x=1
    while [ $n -lt 6 ]
    do
    n=$(( n+1 ))
    echo "sasadgsad gsda $n" >> /home/test/rptest

    if [ $n -eq 5 ]
    then
    while [ $x -le 5 ]
    do
    echo "end of line$x" >> /home/test/rptest
    x=$(( x+1 ))
    done
    fi
    done


    Output this thing



    sasadgsad gsda 1
    sasadgsad gsda 2
    sasadgsad gsda 3
    sasadgsad gsda 4
    sasadgsad gsda 5
    end of line1
    end of line2
    end of line3
    end of line4
    end of line5
    sasadgsad gsda 6


    That 11th line shouldn't be there... Shouldn't the first while thing finish when n = 5? Why does it create that last line?



    Thank you for the help :)



    I even tried it with n=1 and while [ $n -le 5 ]










    share|improve this question

























      up vote
      0
      down vote

      favorite
      1









      up vote
      0
      down vote

      favorite
      1






      1





      This thing here



      n=0
      x=1
      while [ $n -lt 6 ]
      do
      n=$(( n+1 ))
      echo "sasadgsad gsda $n" >> /home/test/rptest

      if [ $n -eq 5 ]
      then
      while [ $x -le 5 ]
      do
      echo "end of line$x" >> /home/test/rptest
      x=$(( x+1 ))
      done
      fi
      done


      Output this thing



      sasadgsad gsda 1
      sasadgsad gsda 2
      sasadgsad gsda 3
      sasadgsad gsda 4
      sasadgsad gsda 5
      end of line1
      end of line2
      end of line3
      end of line4
      end of line5
      sasadgsad gsda 6


      That 11th line shouldn't be there... Shouldn't the first while thing finish when n = 5? Why does it create that last line?



      Thank you for the help :)



      I even tried it with n=1 and while [ $n -le 5 ]










      share|improve this question















      This thing here



      n=0
      x=1
      while [ $n -lt 6 ]
      do
      n=$(( n+1 ))
      echo "sasadgsad gsda $n" >> /home/test/rptest

      if [ $n -eq 5 ]
      then
      while [ $x -le 5 ]
      do
      echo "end of line$x" >> /home/test/rptest
      x=$(( x+1 ))
      done
      fi
      done


      Output this thing



      sasadgsad gsda 1
      sasadgsad gsda 2
      sasadgsad gsda 3
      sasadgsad gsda 4
      sasadgsad gsda 5
      end of line1
      end of line2
      end of line3
      end of line4
      end of line5
      sasadgsad gsda 6


      That 11th line shouldn't be there... Shouldn't the first while thing finish when n = 5? Why does it create that last line?



      Thank you for the help :)



      I even tried it with n=1 and while [ $n -le 5 ]







      linux shell-script






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 7 at 14:22









      Kusalananda

      106k14209327




      106k14209327










      asked Aug 7 at 13:53









      iamAguest

      945




      945




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Your script with proper indentation:



          n=0
          x=1
          while [ $n -lt 6 ]; do
          n=$(( n+1 ))
          echo "sasadgsad gsda $n" >> /home/test/rptest

          if [ $n -eq 5 ]; then
          while [ $x -le 5 ]; do
          echo "end of line$x" >> /home/test/rptest
          x=$(( x+1 ))
          done
          fi
          done


          Your outer loop runs from 0 to 5, which is six times. Since you update n at the start of the outer loop, the value of n will go from 1 to 6 in the body of the loop. When n is 5, you run another loop from 1 to 5 outputting end of line.... When that's done, you still have one iteration of the outer loop to do.



          Another way to write the script in bash:



          for (( n=1; n<=5; ++n )); do
          printf 'sasadgsad gsda %sn' "$n"

          if (( n == 5 )); then
          for (( x=1; x<=5; ++x )); do
          printf 'end of line%sn' "$x"
          done
          fi
          done >>/home/test/rptest


          This would not have the same issue because the outer loop stops when n reaches 6.



          However, if you just want to append the output of the inner loop after the that of the outer, you might just as well run the after each other:



          for (( n=1; n<=5; ++n )); do
          printf 'sasadgsad gsda %sn' "$n"
          done >>/home/test/rptest

          for (( n=1; n<=5; ++x )); do
          printf 'end of line%sn' "$n"
          done >>/home/test/rptest


          or, for this simple example only,



          printf 'sasadgsad gsda %sn' 1..5 >>/home/test/rptest
          printf 'end of line%sn' 1..5 >>/home/test/rptest





          share|improve this answer






















          • Thanks! The "for" version of this is much better, I'm gonna use it from now on :D
            – iamAguest
            Aug 7 at 14:12










          • @iamAguest Just remember that sh may not understand this type of loop, so run your scripts with bash to be sure it works, not sh.
            – Kusalananda
            Aug 7 at 14:13











          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f461076%2fwhat-am-i-doing-wrong-here-issue-with-loops%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          Your script with proper indentation:



          n=0
          x=1
          while [ $n -lt 6 ]; do
          n=$(( n+1 ))
          echo "sasadgsad gsda $n" >> /home/test/rptest

          if [ $n -eq 5 ]; then
          while [ $x -le 5 ]; do
          echo "end of line$x" >> /home/test/rptest
          x=$(( x+1 ))
          done
          fi
          done


          Your outer loop runs from 0 to 5, which is six times. Since you update n at the start of the outer loop, the value of n will go from 1 to 6 in the body of the loop. When n is 5, you run another loop from 1 to 5 outputting end of line.... When that's done, you still have one iteration of the outer loop to do.



          Another way to write the script in bash:



          for (( n=1; n<=5; ++n )); do
          printf 'sasadgsad gsda %sn' "$n"

          if (( n == 5 )); then
          for (( x=1; x<=5; ++x )); do
          printf 'end of line%sn' "$x"
          done
          fi
          done >>/home/test/rptest


          This would not have the same issue because the outer loop stops when n reaches 6.



          However, if you just want to append the output of the inner loop after the that of the outer, you might just as well run the after each other:



          for (( n=1; n<=5; ++n )); do
          printf 'sasadgsad gsda %sn' "$n"
          done >>/home/test/rptest

          for (( n=1; n<=5; ++x )); do
          printf 'end of line%sn' "$n"
          done >>/home/test/rptest


          or, for this simple example only,



          printf 'sasadgsad gsda %sn' 1..5 >>/home/test/rptest
          printf 'end of line%sn' 1..5 >>/home/test/rptest





          share|improve this answer






















          • Thanks! The "for" version of this is much better, I'm gonna use it from now on :D
            – iamAguest
            Aug 7 at 14:12










          • @iamAguest Just remember that sh may not understand this type of loop, so run your scripts with bash to be sure it works, not sh.
            – Kusalananda
            Aug 7 at 14:13















          up vote
          2
          down vote



          accepted










          Your script with proper indentation:



          n=0
          x=1
          while [ $n -lt 6 ]; do
          n=$(( n+1 ))
          echo "sasadgsad gsda $n" >> /home/test/rptest

          if [ $n -eq 5 ]; then
          while [ $x -le 5 ]; do
          echo "end of line$x" >> /home/test/rptest
          x=$(( x+1 ))
          done
          fi
          done


          Your outer loop runs from 0 to 5, which is six times. Since you update n at the start of the outer loop, the value of n will go from 1 to 6 in the body of the loop. When n is 5, you run another loop from 1 to 5 outputting end of line.... When that's done, you still have one iteration of the outer loop to do.



          Another way to write the script in bash:



          for (( n=1; n<=5; ++n )); do
          printf 'sasadgsad gsda %sn' "$n"

          if (( n == 5 )); then
          for (( x=1; x<=5; ++x )); do
          printf 'end of line%sn' "$x"
          done
          fi
          done >>/home/test/rptest


          This would not have the same issue because the outer loop stops when n reaches 6.



          However, if you just want to append the output of the inner loop after the that of the outer, you might just as well run the after each other:



          for (( n=1; n<=5; ++n )); do
          printf 'sasadgsad gsda %sn' "$n"
          done >>/home/test/rptest

          for (( n=1; n<=5; ++x )); do
          printf 'end of line%sn' "$n"
          done >>/home/test/rptest


          or, for this simple example only,



          printf 'sasadgsad gsda %sn' 1..5 >>/home/test/rptest
          printf 'end of line%sn' 1..5 >>/home/test/rptest





          share|improve this answer






















          • Thanks! The "for" version of this is much better, I'm gonna use it from now on :D
            – iamAguest
            Aug 7 at 14:12










          • @iamAguest Just remember that sh may not understand this type of loop, so run your scripts with bash to be sure it works, not sh.
            – Kusalananda
            Aug 7 at 14:13













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          Your script with proper indentation:



          n=0
          x=1
          while [ $n -lt 6 ]; do
          n=$(( n+1 ))
          echo "sasadgsad gsda $n" >> /home/test/rptest

          if [ $n -eq 5 ]; then
          while [ $x -le 5 ]; do
          echo "end of line$x" >> /home/test/rptest
          x=$(( x+1 ))
          done
          fi
          done


          Your outer loop runs from 0 to 5, which is six times. Since you update n at the start of the outer loop, the value of n will go from 1 to 6 in the body of the loop. When n is 5, you run another loop from 1 to 5 outputting end of line.... When that's done, you still have one iteration of the outer loop to do.



          Another way to write the script in bash:



          for (( n=1; n<=5; ++n )); do
          printf 'sasadgsad gsda %sn' "$n"

          if (( n == 5 )); then
          for (( x=1; x<=5; ++x )); do
          printf 'end of line%sn' "$x"
          done
          fi
          done >>/home/test/rptest


          This would not have the same issue because the outer loop stops when n reaches 6.



          However, if you just want to append the output of the inner loop after the that of the outer, you might just as well run the after each other:



          for (( n=1; n<=5; ++n )); do
          printf 'sasadgsad gsda %sn' "$n"
          done >>/home/test/rptest

          for (( n=1; n<=5; ++x )); do
          printf 'end of line%sn' "$n"
          done >>/home/test/rptest


          or, for this simple example only,



          printf 'sasadgsad gsda %sn' 1..5 >>/home/test/rptest
          printf 'end of line%sn' 1..5 >>/home/test/rptest





          share|improve this answer














          Your script with proper indentation:



          n=0
          x=1
          while [ $n -lt 6 ]; do
          n=$(( n+1 ))
          echo "sasadgsad gsda $n" >> /home/test/rptest

          if [ $n -eq 5 ]; then
          while [ $x -le 5 ]; do
          echo "end of line$x" >> /home/test/rptest
          x=$(( x+1 ))
          done
          fi
          done


          Your outer loop runs from 0 to 5, which is six times. Since you update n at the start of the outer loop, the value of n will go from 1 to 6 in the body of the loop. When n is 5, you run another loop from 1 to 5 outputting end of line.... When that's done, you still have one iteration of the outer loop to do.



          Another way to write the script in bash:



          for (( n=1; n<=5; ++n )); do
          printf 'sasadgsad gsda %sn' "$n"

          if (( n == 5 )); then
          for (( x=1; x<=5; ++x )); do
          printf 'end of line%sn' "$x"
          done
          fi
          done >>/home/test/rptest


          This would not have the same issue because the outer loop stops when n reaches 6.



          However, if you just want to append the output of the inner loop after the that of the outer, you might just as well run the after each other:



          for (( n=1; n<=5; ++n )); do
          printf 'sasadgsad gsda %sn' "$n"
          done >>/home/test/rptest

          for (( n=1; n<=5; ++x )); do
          printf 'end of line%sn' "$n"
          done >>/home/test/rptest


          or, for this simple example only,



          printf 'sasadgsad gsda %sn' 1..5 >>/home/test/rptest
          printf 'end of line%sn' 1..5 >>/home/test/rptest






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 7 at 14:24

























          answered Aug 7 at 13:59









          Kusalananda

          106k14209327




          106k14209327











          • Thanks! The "for" version of this is much better, I'm gonna use it from now on :D
            – iamAguest
            Aug 7 at 14:12










          • @iamAguest Just remember that sh may not understand this type of loop, so run your scripts with bash to be sure it works, not sh.
            – Kusalananda
            Aug 7 at 14:13

















          • Thanks! The "for" version of this is much better, I'm gonna use it from now on :D
            – iamAguest
            Aug 7 at 14:12










          • @iamAguest Just remember that sh may not understand this type of loop, so run your scripts with bash to be sure it works, not sh.
            – Kusalananda
            Aug 7 at 14:13
















          Thanks! The "for" version of this is much better, I'm gonna use it from now on :D
          – iamAguest
          Aug 7 at 14:12




          Thanks! The "for" version of this is much better, I'm gonna use it from now on :D
          – iamAguest
          Aug 7 at 14:12












          @iamAguest Just remember that sh may not understand this type of loop, so run your scripts with bash to be sure it works, not sh.
          – Kusalananda
          Aug 7 at 14:13





          @iamAguest Just remember that sh may not understand this type of loop, so run your scripts with bash to be sure it works, not sh.
          – Kusalananda
          Aug 7 at 14:13


















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f461076%2fwhat-am-i-doing-wrong-here-issue-with-loops%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?