How do I exit BASH while loop using modulus operator? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)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 pronounce 伝統色

Project Euler #1 in C++

Crossing US/Canada Border for less than 24 hours

Why are vacuum tubes still used in amateur radios?

Why does it sometimes sound good to play a grace note as a lead in to a note in a melody?

Google .dev domain strangely redirects to https

How do I tell what width chain my used chainring needs?

What is an "asse" in Elizabethan English?

Sum letters are not two different

Lagrange four-squares theorem --- deterministic complexity

How often does castling occur in grandmaster games?

How many morphisms from 1 to 1+1 can there be?

Draw 4 of the same figure in the same tikzpicture

What is the home of the drow in Flanaess?

Hangman Game with C++

Movie where a circus ringmaster turns people into animals

Has negative voting ever been officially implemented in elections, or seriously proposed, or even studied?

What happened to Thoros of Myr's flaming sword?

Antipodal Land Area Calculation

How can I prevent/balance waiting and turtling as a response to cooldown mechanics

Amount of permutations on an NxNxN Rubik's Cube

The Nth Gryphon Number

Can a Beast Master ranger change beast companions?

What would you call this weird metallic apparatus that allows you to lift people?



How do I exit BASH while loop using modulus operator?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)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



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








4















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
    Apr 2 at 18:45











  • the loop does not end when entering 25 @steeldriver

    – Roosevelt Mendieta
    Apr 2 at 18:50

















4















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
    Apr 2 at 18:45











  • the loop does not end when entering 25 @steeldriver

    – Roosevelt Mendieta
    Apr 2 at 18:50













4












4








4








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 Apr 2 at 19:09







Roosevelt Mendieta

















asked Apr 2 at 18:04









Roosevelt MendietaRoosevelt Mendieta

4915




4915







  • 2





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

    – steeldriver
    Apr 2 at 18:45











  • the loop does not end when entering 25 @steeldriver

    – Roosevelt Mendieta
    Apr 2 at 18:50












  • 2





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

    – steeldriver
    Apr 2 at 18:45











  • the loop does not end when entering 25 @steeldriver

    – Roosevelt Mendieta
    Apr 2 at 18:50







2




2





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

– steeldriver
Apr 2 at 18:45





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

– steeldriver
Apr 2 at 18:45













the loop does not end when entering 25 @steeldriver

– Roosevelt Mendieta
Apr 2 at 18:50





the loop does not end when entering 25 @steeldriver

– Roosevelt Mendieta
Apr 2 at 18:50










3 Answers
3






active

oldest

votes


















7














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
    Apr 2 at 18:46






  • 6





    @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
    Apr 2 at 18:58







  • 2





    @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
    Apr 2 at 19:04











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

    – Kulfy
    Apr 2 at 19:07











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

    – Roosevelt Mendieta
    Apr 2 at 19:10


















5














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














    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









      7














      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
        Apr 2 at 18:46






      • 6





        @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
        Apr 2 at 18:58







      • 2





        @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
        Apr 2 at 19:04











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

        – Kulfy
        Apr 2 at 19:07











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

        – Roosevelt Mendieta
        Apr 2 at 19:10















      7














      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
        Apr 2 at 18:46






      • 6





        @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
        Apr 2 at 18:58







      • 2





        @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
        Apr 2 at 19:04











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

        – Kulfy
        Apr 2 at 19:07











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

        – Roosevelt Mendieta
        Apr 2 at 19:10













      7












      7








      7







      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 Apr 2 at 18:16









      PerlDuckPerlDuck

      8,04111637




      8,04111637












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

        – Roosevelt Mendieta
        Apr 2 at 18:46






      • 6





        @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
        Apr 2 at 18:58







      • 2





        @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
        Apr 2 at 19:04











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

        – Kulfy
        Apr 2 at 19:07











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

        – Roosevelt Mendieta
        Apr 2 at 19:10

















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

        – Roosevelt Mendieta
        Apr 2 at 18:46






      • 6





        @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
        Apr 2 at 18:58







      • 2





        @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
        Apr 2 at 19:04











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

        – Kulfy
        Apr 2 at 19:07











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

        – Roosevelt Mendieta
        Apr 2 at 19:10
















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

      – Roosevelt Mendieta
      Apr 2 at 18:46





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

      – Roosevelt Mendieta
      Apr 2 at 18:46




      6




      6





      @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
      Apr 2 at 18:58






      @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
      Apr 2 at 18:58





      2




      2





      @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
      Apr 2 at 19:04





      @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
      Apr 2 at 19:04













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

      – Kulfy
      Apr 2 at 19:07





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

      – Kulfy
      Apr 2 at 19:07













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

      – Roosevelt Mendieta
      Apr 2 at 19:10





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

      – Roosevelt Mendieta
      Apr 2 at 19:10













      5














      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





























        5














        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



























          5












          5








          5







          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 Apr 2 at 19:14

























          answered Apr 2 at 19:03









          sudodussudodus

          25.8k33178




          25.8k33178





















              4














              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





























                4














                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



























                  4












                  4








                  4







                  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 Apr 2 at 18:59

























                  answered Apr 2 at 18:54









                  Sergiy KolodyazhnyySergiy Kolodyazhnyy

                  75.6k9156331




                  75.6k9156331



























                      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

                      Adding axes to figuresAdding axes labels to LaTeX figuresLaTeX equivalent of ConTeXt buffersRotate a node but not its content: the case of the ellipse decorationHow to define the default vertical distance between nodes?TikZ scaling graphic and adjust node position and keep font sizeNumerical conditional within tikz keys?adding axes to shapesAlign axes across subfiguresAdding figures with a certain orderLine up nested tikz enviroments or how to get rid of themAdding axes labels to LaTeX figures

                      Tähtien Talli Jäsenet | Lähteet | NavigointivalikkoSuomen Hippos – Tähtien Talli

                      Do these cracks on my tires look bad? The Next CEO of Stack OverflowDry rot tire should I replace?Having to replace tiresFishtailed so easily? Bad tires? ABS?Filling the tires with something other than air, to avoid puncture hassles?Used Michelin tires safe to install?Do these tyre cracks necessitate replacement?Rumbling noise: tires or mechanicalIs it possible to fix noisy feathered tires?Are bad winter tires still better than summer tires in winter?Torque converter failure - Related to replacing only 2 tires?Why use snow tires on all 4 wheels on 2-wheel-drive cars?