Loop to print odd numbers not printing as many as it should












0















I'm writing a script that prints out a user-provided amount of odd numbers starting from a user-provided number.



So for an example if you would enter that you want to print out 5 numbers starting from the number 3, it would output 3, 5, 7, 9, and 11.



I'm currently trying to use the following code:



echo "Enter how many numbers you want to print"
read n
echo "Enter the first number"
read a

for ((a; a < n; a++)); do
((b = a % 2))
if [ $b -ne 0 ]; then
echo "$a"
fi
done


However, with n=5; a=3, the output is not the expected 3 5 7 9 11 but is instead only 3.










share|improve this question

























  • ...so, what's the part that doesn't actually work? See Minimal, Complete, and Verifiable example guidelines -- a question should have a specific problem (not the larger problem your script tries to solve, but the problem with your script), and the shortest code that lets others see that problem.

    – Charles Duffy
    Nov 22 '18 at 17:31











  • BTW, a good place to start is logging with set -x. See your code running at ideone.com/hgCZDP, with a log in the "stderr" section.

    – Charles Duffy
    Nov 22 '18 at 17:35






  • 2





    ...in the current case, what you have isn't a bash-the-language bug, but a thinking-about-your-problem bug: You're comparing a < n, but a doesn't start at 0, it starts at the value the user entered, so it doesn't print n numbers; instead, it just prints odd numbers greater than or equal to a and less than n. For the examples n=5 and a=3, the only odd number that meets that criteria is 3, so that's all it prints.

    – Charles Duffy
    Nov 22 '18 at 17:36


















0















I'm writing a script that prints out a user-provided amount of odd numbers starting from a user-provided number.



So for an example if you would enter that you want to print out 5 numbers starting from the number 3, it would output 3, 5, 7, 9, and 11.



I'm currently trying to use the following code:



echo "Enter how many numbers you want to print"
read n
echo "Enter the first number"
read a

for ((a; a < n; a++)); do
((b = a % 2))
if [ $b -ne 0 ]; then
echo "$a"
fi
done


However, with n=5; a=3, the output is not the expected 3 5 7 9 11 but is instead only 3.










share|improve this question

























  • ...so, what's the part that doesn't actually work? See Minimal, Complete, and Verifiable example guidelines -- a question should have a specific problem (not the larger problem your script tries to solve, but the problem with your script), and the shortest code that lets others see that problem.

    – Charles Duffy
    Nov 22 '18 at 17:31











  • BTW, a good place to start is logging with set -x. See your code running at ideone.com/hgCZDP, with a log in the "stderr" section.

    – Charles Duffy
    Nov 22 '18 at 17:35






  • 2





    ...in the current case, what you have isn't a bash-the-language bug, but a thinking-about-your-problem bug: You're comparing a < n, but a doesn't start at 0, it starts at the value the user entered, so it doesn't print n numbers; instead, it just prints odd numbers greater than or equal to a and less than n. For the examples n=5 and a=3, the only odd number that meets that criteria is 3, so that's all it prints.

    – Charles Duffy
    Nov 22 '18 at 17:36
















0












0








0








I'm writing a script that prints out a user-provided amount of odd numbers starting from a user-provided number.



So for an example if you would enter that you want to print out 5 numbers starting from the number 3, it would output 3, 5, 7, 9, and 11.



I'm currently trying to use the following code:



echo "Enter how many numbers you want to print"
read n
echo "Enter the first number"
read a

for ((a; a < n; a++)); do
((b = a % 2))
if [ $b -ne 0 ]; then
echo "$a"
fi
done


However, with n=5; a=3, the output is not the expected 3 5 7 9 11 but is instead only 3.










share|improve this question
















I'm writing a script that prints out a user-provided amount of odd numbers starting from a user-provided number.



So for an example if you would enter that you want to print out 5 numbers starting from the number 3, it would output 3, 5, 7, 9, and 11.



I'm currently trying to use the following code:



echo "Enter how many numbers you want to print"
read n
echo "Enter the first number"
read a

for ((a; a < n; a++)); do
((b = a % 2))
if [ $b -ne 0 ]; then
echo "$a"
fi
done


However, with n=5; a=3, the output is not the expected 3 5 7 9 11 but is instead only 3.







bash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 17:41









Charles Duffy

180k26205260




180k26205260










asked Nov 22 '18 at 17:26









FrostbyteeFrostbytee

11




11













  • ...so, what's the part that doesn't actually work? See Minimal, Complete, and Verifiable example guidelines -- a question should have a specific problem (not the larger problem your script tries to solve, but the problem with your script), and the shortest code that lets others see that problem.

    – Charles Duffy
    Nov 22 '18 at 17:31











  • BTW, a good place to start is logging with set -x. See your code running at ideone.com/hgCZDP, with a log in the "stderr" section.

    – Charles Duffy
    Nov 22 '18 at 17:35






  • 2





    ...in the current case, what you have isn't a bash-the-language bug, but a thinking-about-your-problem bug: You're comparing a < n, but a doesn't start at 0, it starts at the value the user entered, so it doesn't print n numbers; instead, it just prints odd numbers greater than or equal to a and less than n. For the examples n=5 and a=3, the only odd number that meets that criteria is 3, so that's all it prints.

    – Charles Duffy
    Nov 22 '18 at 17:36





















  • ...so, what's the part that doesn't actually work? See Minimal, Complete, and Verifiable example guidelines -- a question should have a specific problem (not the larger problem your script tries to solve, but the problem with your script), and the shortest code that lets others see that problem.

    – Charles Duffy
    Nov 22 '18 at 17:31











  • BTW, a good place to start is logging with set -x. See your code running at ideone.com/hgCZDP, with a log in the "stderr" section.

    – Charles Duffy
    Nov 22 '18 at 17:35






  • 2





    ...in the current case, what you have isn't a bash-the-language bug, but a thinking-about-your-problem bug: You're comparing a < n, but a doesn't start at 0, it starts at the value the user entered, so it doesn't print n numbers; instead, it just prints odd numbers greater than or equal to a and less than n. For the examples n=5 and a=3, the only odd number that meets that criteria is 3, so that's all it prints.

    – Charles Duffy
    Nov 22 '18 at 17:36



















...so, what's the part that doesn't actually work? See Minimal, Complete, and Verifiable example guidelines -- a question should have a specific problem (not the larger problem your script tries to solve, but the problem with your script), and the shortest code that lets others see that problem.

– Charles Duffy
Nov 22 '18 at 17:31





...so, what's the part that doesn't actually work? See Minimal, Complete, and Verifiable example guidelines -- a question should have a specific problem (not the larger problem your script tries to solve, but the problem with your script), and the shortest code that lets others see that problem.

– Charles Duffy
Nov 22 '18 at 17:31













BTW, a good place to start is logging with set -x. See your code running at ideone.com/hgCZDP, with a log in the "stderr" section.

– Charles Duffy
Nov 22 '18 at 17:35





BTW, a good place to start is logging with set -x. See your code running at ideone.com/hgCZDP, with a log in the "stderr" section.

– Charles Duffy
Nov 22 '18 at 17:35




2




2





...in the current case, what you have isn't a bash-the-language bug, but a thinking-about-your-problem bug: You're comparing a < n, but a doesn't start at 0, it starts at the value the user entered, so it doesn't print n numbers; instead, it just prints odd numbers greater than or equal to a and less than n. For the examples n=5 and a=3, the only odd number that meets that criteria is 3, so that's all it prints.

– Charles Duffy
Nov 22 '18 at 17:36







...in the current case, what you have isn't a bash-the-language bug, but a thinking-about-your-problem bug: You're comparing a < n, but a doesn't start at 0, it starts at the value the user entered, so it doesn't print n numbers; instead, it just prints odd numbers greater than or equal to a and less than n. For the examples n=5 and a=3, the only odd number that meets that criteria is 3, so that's all it prints.

– Charles Duffy
Nov 22 '18 at 17:36














1 Answer
1






active

oldest

votes


















0














This is a logic error, rather than a problem using bash. If you want to print n numbers, the easiest way to make sure that happens is to iterate from 0 to n, as follows:



#!/usr/bin/env bash
n=5; a=3 # of course, you can also read from the user.

if ((a % 2 == 0)); then # if our starting number is even...
(( ++a )) # add 1 to make it odd.
fi

for ((i=0; i<n; i++)); do # iterate from 0 to n...
echo "$((a + i*2))" # ...emitting 2*i+a each time.
done





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    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%2fstackoverflow.com%2fquestions%2f53435831%2floop-to-print-odd-numbers-not-printing-as-many-as-it-should%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    This is a logic error, rather than a problem using bash. If you want to print n numbers, the easiest way to make sure that happens is to iterate from 0 to n, as follows:



    #!/usr/bin/env bash
    n=5; a=3 # of course, you can also read from the user.

    if ((a % 2 == 0)); then # if our starting number is even...
    (( ++a )) # add 1 to make it odd.
    fi

    for ((i=0; i<n; i++)); do # iterate from 0 to n...
    echo "$((a + i*2))" # ...emitting 2*i+a each time.
    done





    share|improve this answer






























      0














      This is a logic error, rather than a problem using bash. If you want to print n numbers, the easiest way to make sure that happens is to iterate from 0 to n, as follows:



      #!/usr/bin/env bash
      n=5; a=3 # of course, you can also read from the user.

      if ((a % 2 == 0)); then # if our starting number is even...
      (( ++a )) # add 1 to make it odd.
      fi

      for ((i=0; i<n; i++)); do # iterate from 0 to n...
      echo "$((a + i*2))" # ...emitting 2*i+a each time.
      done





      share|improve this answer




























        0












        0








        0







        This is a logic error, rather than a problem using bash. If you want to print n numbers, the easiest way to make sure that happens is to iterate from 0 to n, as follows:



        #!/usr/bin/env bash
        n=5; a=3 # of course, you can also read from the user.

        if ((a % 2 == 0)); then # if our starting number is even...
        (( ++a )) # add 1 to make it odd.
        fi

        for ((i=0; i<n; i++)); do # iterate from 0 to n...
        echo "$((a + i*2))" # ...emitting 2*i+a each time.
        done





        share|improve this answer















        This is a logic error, rather than a problem using bash. If you want to print n numbers, the easiest way to make sure that happens is to iterate from 0 to n, as follows:



        #!/usr/bin/env bash
        n=5; a=3 # of course, you can also read from the user.

        if ((a % 2 == 0)); then # if our starting number is even...
        (( ++a )) # add 1 to make it odd.
        fi

        for ((i=0; i<n; i++)); do # iterate from 0 to n...
        echo "$((a + i*2))" # ...emitting 2*i+a each time.
        done






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 '18 at 18:27


























        community wiki





        2 revs, 2 users 92%
        Charles Duffy

































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • 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%2fstackoverflow.com%2fquestions%2f53435831%2floop-to-print-odd-numbers-not-printing-as-many-as-it-should%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







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini