How do I exit BASH while loop using modulus operator?Running bash loopWhile loop in ShellCan you help me to understand this explanation of shell quoting?For loop syntax bash scriptExit terminal after running a bash scriptRedirection operator priority in BashDisplay images in a loop using bashMeaning of exit 0, exit 1 and exit 2 in a bash scriptBash - add zero to single digit in while loopRunning a Bash while loop over all similar files

How to show a landlord what we have in savings?

Do Iron Man suits sport waste management systems?

Obtaining database information and values in extended properties

Why do I get negative height?

In Bayesian inference, why are some terms dropped from the posterior predictive?

What is required to make GPS signals available indoors?

How does a dynamic QR code work?

How do I exit BASH while loop using modulus operator?

Why are UK visa biometrics appointments suspended at USCIS Application Support Centers?

How to compactly explain secondary and tertiary characters without resorting to stereotypes?

how do we prove that a sum of two periods is still a period?

Unlock My Phone! February 2018

Does Dispel Magic work on Tiny Hut?

Why is the sentence "Das ist eine Nase" correct?

How dangerous is XSS

How to calculate the right interval for a timelapse on a boat

Why is it a bad idea to hire a hitman to eliminate most corrupt politicians?

Can I hook these wires up to find the connection to a dead outlet?

Why were 5.25" floppy drives cheaper than 8"?

Are British MPs missing the point, with these 'Indicative Votes'?

How seriously should I take size and weight limits of hand luggage?

Mathematica command that allows it to read my intentions

Can compressed videos be decoded back to their uncompresed original format?

What's the meaning of "Sollensaussagen"?



How do I exit BASH while loop using modulus operator?


Running bash loopWhile loop in ShellCan you help me to understand this explanation of shell quoting?For loop syntax bash scriptExit terminal after running a bash scriptRedirection operator priority in BashDisplay images in a loop using bashMeaning of exit 0, exit 1 and exit 2 in a bash scriptBash - add zero to single digit in while loopRunning a Bash while loop over all similar files













2















So practically for my assignment I need to break out of a true while loop when the user inputs a number that gives a modulus remainder of 0, ex: (25 % 5 = 0 break loop) Where in my attempt below have I gone wrong?



while true
do
echo "Please input anything here: "
read INPUT

if [ `expr $INPUT % 5` -eq 0 ]; then
echo "you entered wrong"
else
echo "you entered right"
break
fi
done









share|improve this question



















  • 2





    If the assignment specifies bash, then you might consider using its built-in arithmetic expansion syntax e.g. (( INPUT % 5 == 0 ))

    – steeldriver
    2 hours ago











  • the loop does not end when entering 25 @steeldriver

    – Roosevelt Mendieta
    2 hours ago















2















So practically for my assignment I need to break out of a true while loop when the user inputs a number that gives a modulus remainder of 0, ex: (25 % 5 = 0 break loop) Where in my attempt below have I gone wrong?



while true
do
echo "Please input anything here: "
read INPUT

if [ `expr $INPUT % 5` -eq 0 ]; then
echo "you entered wrong"
else
echo "you entered right"
break
fi
done









share|improve this question



















  • 2





    If the assignment specifies bash, then you might consider using its built-in arithmetic expansion syntax e.g. (( INPUT % 5 == 0 ))

    – steeldriver
    2 hours ago











  • the loop does not end when entering 25 @steeldriver

    – Roosevelt Mendieta
    2 hours ago













2












2








2








So practically for my assignment I need to break out of a true while loop when the user inputs a number that gives a modulus remainder of 0, ex: (25 % 5 = 0 break loop) Where in my attempt below have I gone wrong?



while true
do
echo "Please input anything here: "
read INPUT

if [ `expr $INPUT % 5` -eq 0 ]; then
echo "you entered wrong"
else
echo "you entered right"
break
fi
done









share|improve this question
















So practically for my assignment I need to break out of a true while loop when the user inputs a number that gives a modulus remainder of 0, ex: (25 % 5 = 0 break loop) Where in my attempt below have I gone wrong?



while true
do
echo "Please input anything here: "
read INPUT

if [ `expr $INPUT % 5` -eq 0 ]; then
echo "you entered wrong"
else
echo "you entered right"
break
fi
done






command-line bash scripts






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 hours ago







Roosevelt Mendieta

















asked 3 hours ago









Roosevelt MendietaRoosevelt Mendieta

3915




3915







  • 2





    If the assignment specifies bash, then you might consider using its built-in arithmetic expansion syntax e.g. (( INPUT % 5 == 0 ))

    – steeldriver
    2 hours ago











  • the loop does not end when entering 25 @steeldriver

    – Roosevelt Mendieta
    2 hours ago












  • 2





    If the assignment specifies bash, then you might consider using its built-in arithmetic expansion syntax e.g. (( INPUT % 5 == 0 ))

    – steeldriver
    2 hours ago











  • the loop does not end when entering 25 @steeldriver

    – Roosevelt Mendieta
    2 hours ago







2




2





If the assignment specifies bash, then you might consider using its built-in arithmetic expansion syntax e.g. (( INPUT % 5 == 0 ))

– steeldriver
2 hours ago





If the assignment specifies bash, then you might consider using its built-in arithmetic expansion syntax e.g. (( INPUT % 5 == 0 ))

– steeldriver
2 hours ago













the loop does not end when entering 25 @steeldriver

– Roosevelt Mendieta
2 hours ago





the loop does not end when entering 25 @steeldriver

– Roosevelt Mendieta
2 hours ago










3 Answers
3






active

oldest

votes


















3














Move the break from the else part to the if part:



#!/bin/bash

while true
do
echo "Please input anything here: "
read INPUT

if [ `expr $INPUT % 5` -eq 0 ]; then
echo "you entered wrong"
break
else
echo "you entered right"
fi
done





share|improve this answer























  • this doesn't work, when I enter 40 the code exits

    – Roosevelt Mendieta
    2 hours ago






  • 2





    @RooseveltMendieta Isn't it what you want? I need to break out of a true while loop when the user inputs a number that gives a modulus remainder of 0. 40%5 is also 0.

    – Kulfy
    2 hours ago












  • @Kulfy i was thinking of division in my head instead of modulus, how embarrassing lol yes this solution in fact does work and is exactly what I needed. I need to go to sleep i've been up to late working on this assignment.

    – Roosevelt Mendieta
    2 hours ago











  • @RooseveltMendieta It seems that you changed the original code in your question. So, PerlDuck might need to modify explanation of the answer.

    – Kulfy
    2 hours ago











  • i version controlled the code back to it's original state @Kulfy

    – Roosevelt Mendieta
    2 hours ago


















4














It works for me according to @steeldriver's tips,




  • make sure you use bash



    #!/bin/bash



  • use the bash syntax for arithmetic evaluation



    ((...))


Otherwise the shellscript can remain the same,



#!/bin/bash

while true
do
echo "Please input anything here: "
read INPUT

if (( INPUT % 5 == 0 )) ; then
echo "you entered right"
break
else
echo "you entered wrong"
fi
done


Edit: You have modified the question. This answer corresponds to a previous version of the question. (It is not clear to me, if you want to break the loop, when there is no remainder or when there is a remainder.)






share|improve this answer
































    3














    Since this is bash script we're talking about, you may want to use read -p and arithmetic evaluation ((...))



    $ while read -p "Enter number:" input ; do (( input%5 == 0 )) && echo "Wrong"; break; || echo "alright"; done
    Enter number:11
    alright
    Enter number:7
    alright
    Enter number:10
    Wrong


    Portably, you might want to use [ aka test



    $ [ $((25%5)) -eq 0 ] && echo "Zero"
    Zero
    $ [ $((26%5)) -eq 0 ] && echo "Zero"
    $





    share|improve this answer

























      Your Answer








      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "89"
      ;
      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',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1130696%2fhow-do-i-exit-bash-while-loop-using-modulus-operator%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      Move the break from the else part to the if part:



      #!/bin/bash

      while true
      do
      echo "Please input anything here: "
      read INPUT

      if [ `expr $INPUT % 5` -eq 0 ]; then
      echo "you entered wrong"
      break
      else
      echo "you entered right"
      fi
      done





      share|improve this answer























      • this doesn't work, when I enter 40 the code exits

        – Roosevelt Mendieta
        2 hours ago






      • 2





        @RooseveltMendieta Isn't it what you want? I need to break out of a true while loop when the user inputs a number that gives a modulus remainder of 0. 40%5 is also 0.

        – Kulfy
        2 hours ago












      • @Kulfy i was thinking of division in my head instead of modulus, how embarrassing lol yes this solution in fact does work and is exactly what I needed. I need to go to sleep i've been up to late working on this assignment.

        – Roosevelt Mendieta
        2 hours ago











      • @RooseveltMendieta It seems that you changed the original code in your question. So, PerlDuck might need to modify explanation of the answer.

        – Kulfy
        2 hours ago











      • i version controlled the code back to it's original state @Kulfy

        – Roosevelt Mendieta
        2 hours ago















      3














      Move the break from the else part to the if part:



      #!/bin/bash

      while true
      do
      echo "Please input anything here: "
      read INPUT

      if [ `expr $INPUT % 5` -eq 0 ]; then
      echo "you entered wrong"
      break
      else
      echo "you entered right"
      fi
      done





      share|improve this answer























      • this doesn't work, when I enter 40 the code exits

        – Roosevelt Mendieta
        2 hours ago






      • 2





        @RooseveltMendieta Isn't it what you want? I need to break out of a true while loop when the user inputs a number that gives a modulus remainder of 0. 40%5 is also 0.

        – Kulfy
        2 hours ago












      • @Kulfy i was thinking of division in my head instead of modulus, how embarrassing lol yes this solution in fact does work and is exactly what I needed. I need to go to sleep i've been up to late working on this assignment.

        – Roosevelt Mendieta
        2 hours ago











      • @RooseveltMendieta It seems that you changed the original code in your question. So, PerlDuck might need to modify explanation of the answer.

        – Kulfy
        2 hours ago











      • i version controlled the code back to it's original state @Kulfy

        – Roosevelt Mendieta
        2 hours ago













      3












      3








      3







      Move the break from the else part to the if part:



      #!/bin/bash

      while true
      do
      echo "Please input anything here: "
      read INPUT

      if [ `expr $INPUT % 5` -eq 0 ]; then
      echo "you entered wrong"
      break
      else
      echo "you entered right"
      fi
      done





      share|improve this answer













      Move the break from the else part to the if part:



      #!/bin/bash

      while true
      do
      echo "Please input anything here: "
      read INPUT

      if [ `expr $INPUT % 5` -eq 0 ]; then
      echo "you entered wrong"
      break
      else
      echo "you entered right"
      fi
      done






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered 3 hours ago









      PerlDuckPerlDuck

      7,90611636




      7,90611636












      • this doesn't work, when I enter 40 the code exits

        – Roosevelt Mendieta
        2 hours ago






      • 2





        @RooseveltMendieta Isn't it what you want? I need to break out of a true while loop when the user inputs a number that gives a modulus remainder of 0. 40%5 is also 0.

        – Kulfy
        2 hours ago












      • @Kulfy i was thinking of division in my head instead of modulus, how embarrassing lol yes this solution in fact does work and is exactly what I needed. I need to go to sleep i've been up to late working on this assignment.

        – Roosevelt Mendieta
        2 hours ago











      • @RooseveltMendieta It seems that you changed the original code in your question. So, PerlDuck might need to modify explanation of the answer.

        – Kulfy
        2 hours ago











      • i version controlled the code back to it's original state @Kulfy

        – Roosevelt Mendieta
        2 hours ago

















      • this doesn't work, when I enter 40 the code exits

        – Roosevelt Mendieta
        2 hours ago






      • 2





        @RooseveltMendieta Isn't it what you want? I need to break out of a true while loop when the user inputs a number that gives a modulus remainder of 0. 40%5 is also 0.

        – Kulfy
        2 hours ago












      • @Kulfy i was thinking of division in my head instead of modulus, how embarrassing lol yes this solution in fact does work and is exactly what I needed. I need to go to sleep i've been up to late working on this assignment.

        – Roosevelt Mendieta
        2 hours ago











      • @RooseveltMendieta It seems that you changed the original code in your question. So, PerlDuck might need to modify explanation of the answer.

        – Kulfy
        2 hours ago











      • i version controlled the code back to it's original state @Kulfy

        – Roosevelt Mendieta
        2 hours ago
















      this doesn't work, when I enter 40 the code exits

      – Roosevelt Mendieta
      2 hours ago





      this doesn't work, when I enter 40 the code exits

      – Roosevelt Mendieta
      2 hours ago




      2




      2





      @RooseveltMendieta Isn't it what you want? I need to break out of a true while loop when the user inputs a number that gives a modulus remainder of 0. 40%5 is also 0.

      – Kulfy
      2 hours ago






      @RooseveltMendieta Isn't it what you want? I need to break out of a true while loop when the user inputs a number that gives a modulus remainder of 0. 40%5 is also 0.

      – Kulfy
      2 hours ago














      @Kulfy i was thinking of division in my head instead of modulus, how embarrassing lol yes this solution in fact does work and is exactly what I needed. I need to go to sleep i've been up to late working on this assignment.

      – Roosevelt Mendieta
      2 hours ago





      @Kulfy i was thinking of division in my head instead of modulus, how embarrassing lol yes this solution in fact does work and is exactly what I needed. I need to go to sleep i've been up to late working on this assignment.

      – Roosevelt Mendieta
      2 hours ago













      @RooseveltMendieta It seems that you changed the original code in your question. So, PerlDuck might need to modify explanation of the answer.

      – Kulfy
      2 hours ago





      @RooseveltMendieta It seems that you changed the original code in your question. So, PerlDuck might need to modify explanation of the answer.

      – Kulfy
      2 hours ago













      i version controlled the code back to it's original state @Kulfy

      – Roosevelt Mendieta
      2 hours ago





      i version controlled the code back to it's original state @Kulfy

      – Roosevelt Mendieta
      2 hours ago













      4














      It works for me according to @steeldriver's tips,




      • make sure you use bash



        #!/bin/bash



      • use the bash syntax for arithmetic evaluation



        ((...))


      Otherwise the shellscript can remain the same,



      #!/bin/bash

      while true
      do
      echo "Please input anything here: "
      read INPUT

      if (( INPUT % 5 == 0 )) ; then
      echo "you entered right"
      break
      else
      echo "you entered wrong"
      fi
      done


      Edit: You have modified the question. This answer corresponds to a previous version of the question. (It is not clear to me, if you want to break the loop, when there is no remainder or when there is a remainder.)






      share|improve this answer





























        4














        It works for me according to @steeldriver's tips,




        • make sure you use bash



          #!/bin/bash



        • use the bash syntax for arithmetic evaluation



          ((...))


        Otherwise the shellscript can remain the same,



        #!/bin/bash

        while true
        do
        echo "Please input anything here: "
        read INPUT

        if (( INPUT % 5 == 0 )) ; then
        echo "you entered right"
        break
        else
        echo "you entered wrong"
        fi
        done


        Edit: You have modified the question. This answer corresponds to a previous version of the question. (It is not clear to me, if you want to break the loop, when there is no remainder or when there is a remainder.)






        share|improve this answer



























          4












          4








          4







          It works for me according to @steeldriver's tips,




          • make sure you use bash



            #!/bin/bash



          • use the bash syntax for arithmetic evaluation



            ((...))


          Otherwise the shellscript can remain the same,



          #!/bin/bash

          while true
          do
          echo "Please input anything here: "
          read INPUT

          if (( INPUT % 5 == 0 )) ; then
          echo "you entered right"
          break
          else
          echo "you entered wrong"
          fi
          done


          Edit: You have modified the question. This answer corresponds to a previous version of the question. (It is not clear to me, if you want to break the loop, when there is no remainder or when there is a remainder.)






          share|improve this answer















          It works for me according to @steeldriver's tips,




          • make sure you use bash



            #!/bin/bash



          • use the bash syntax for arithmetic evaluation



            ((...))


          Otherwise the shellscript can remain the same,



          #!/bin/bash

          while true
          do
          echo "Please input anything here: "
          read INPUT

          if (( INPUT % 5 == 0 )) ; then
          echo "you entered right"
          break
          else
          echo "you entered wrong"
          fi
          done


          Edit: You have modified the question. This answer corresponds to a previous version of the question. (It is not clear to me, if you want to break the loop, when there is no remainder or when there is a remainder.)







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 hours ago

























          answered 2 hours ago









          sudodussudodus

          25.6k33078




          25.6k33078





















              3














              Since this is bash script we're talking about, you may want to use read -p and arithmetic evaluation ((...))



              $ while read -p "Enter number:" input ; do (( input%5 == 0 )) && echo "Wrong"; break; || echo "alright"; done
              Enter number:11
              alright
              Enter number:7
              alright
              Enter number:10
              Wrong


              Portably, you might want to use [ aka test



              $ [ $((25%5)) -eq 0 ] && echo "Zero"
              Zero
              $ [ $((26%5)) -eq 0 ] && echo "Zero"
              $





              share|improve this answer





























                3














                Since this is bash script we're talking about, you may want to use read -p and arithmetic evaluation ((...))



                $ while read -p "Enter number:" input ; do (( input%5 == 0 )) && echo "Wrong"; break; || echo "alright"; done
                Enter number:11
                alright
                Enter number:7
                alright
                Enter number:10
                Wrong


                Portably, you might want to use [ aka test



                $ [ $((25%5)) -eq 0 ] && echo "Zero"
                Zero
                $ [ $((26%5)) -eq 0 ] && echo "Zero"
                $





                share|improve this answer



























                  3












                  3








                  3







                  Since this is bash script we're talking about, you may want to use read -p and arithmetic evaluation ((...))



                  $ while read -p "Enter number:" input ; do (( input%5 == 0 )) && echo "Wrong"; break; || echo "alright"; done
                  Enter number:11
                  alright
                  Enter number:7
                  alright
                  Enter number:10
                  Wrong


                  Portably, you might want to use [ aka test



                  $ [ $((25%5)) -eq 0 ] && echo "Zero"
                  Zero
                  $ [ $((26%5)) -eq 0 ] && echo "Zero"
                  $





                  share|improve this answer















                  Since this is bash script we're talking about, you may want to use read -p and arithmetic evaluation ((...))



                  $ while read -p "Enter number:" input ; do (( input%5 == 0 )) && echo "Wrong"; break; || echo "alright"; done
                  Enter number:11
                  alright
                  Enter number:7
                  alright
                  Enter number:10
                  Wrong


                  Portably, you might want to use [ aka test



                  $ [ $((25%5)) -eq 0 ] && echo "Zero"
                  Zero
                  $ [ $((26%5)) -eq 0 ] && echo "Zero"
                  $






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 2 hours ago

























                  answered 2 hours ago









                  Sergiy KolodyazhnyySergiy Kolodyazhnyy

                  74.8k9155325




                  74.8k9155325



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Ask Ubuntu!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1130696%2fhow-do-i-exit-bash-while-loop-using-modulus-operator%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Ружовы пелікан Змест Знешні выгляд | Пашырэнне | Асаблівасці біялогіі | Літаратура | НавігацыяДагледжаная версіяправерана1 зменаДагледжаная версіяправерана1 змена/ 22697590 Сістэматыкана ВіківідахВыявына Вікісховішчы174693363011049382

                      ValueError: Error when checking input: expected conv2d_13_input to have shape (3, 150, 150) but got array with shape (150, 150, 3)2019 Community Moderator ElectionError when checking : expected dense_1_input to have shape (None, 5) but got array with shape (200, 1)Error 'Expected 2D array, got 1D array instead:'ValueError: Error when checking input: expected lstm_41_input to have 3 dimensions, but got array with shape (40000,100)ValueError: Error when checking target: expected dense_1 to have shape (7,) but got array with shape (1,)ValueError: Error when checking target: expected dense_2 to have shape (1,) but got array with shape (0,)Keras exception: ValueError: Error when checking input: expected conv2d_1_input to have shape (150, 150, 3) but got array with shape (256, 256, 3)Steps taking too long to completewhen checking input: expected dense_1_input to have shape (13328,) but got array with shape (317,)ValueError: Error when checking target: expected dense_3 to have shape (None, 1) but got array with shape (7715, 40000)Keras exception: Error when checking input: expected dense_input to have shape (2,) but got array with shape (1,)

                      Illegal assignment from SObject to ContactFetching String, Id from Map - Illegal Assignment Id to Field / ObjectError: Compile Error: Illegal assignment from String to BooleanError: List has no rows for assignment to SObjectError on Test Class - System.QueryException: List has no rows for assignment to SObjectRemote action problemDML requires SObject or SObject list type error“Illegal assignment from List to List”Test Class Fail: Batch Class: System.QueryException: List has no rows for assignment to SObjectMapping to a user'List has no rows for assignment to SObject' Mystery