Different outputs when using break in bash












-1














I wrote a simple script:



for DIR in /etc/*; do
if [ "$DIR" == "/etc/dhcp" ]; then
echo $DIR
break
fi
done


This works correctly but if I use break after fi it doesn't generate the desirable result.



What is the difference between that script and this one?



for DIR in /etc/*; do
if [ "$DIR" == "/etc/dhcp" ]; then
echo $DIR
fi
break
done









share|improve this question
























  • I edited your post and added some indentation. Feel free to revert it if you don't like it. But to my mind it is now clearer that in the second case the break is outside the if.
    – PerlDuck
    Dec 16 '18 at 11:43








  • 2




    @PerlDuck and that's why indentation is important. :P
    – Kulfy
    Dec 16 '18 at 12:38










  • What were the different outputs? And what were the inputs? Ah well, I suppose it doesn't really matter, maybe the title could be better
    – Xen2050
    Dec 17 '18 at 15:32
















-1














I wrote a simple script:



for DIR in /etc/*; do
if [ "$DIR" == "/etc/dhcp" ]; then
echo $DIR
break
fi
done


This works correctly but if I use break after fi it doesn't generate the desirable result.



What is the difference between that script and this one?



for DIR in /etc/*; do
if [ "$DIR" == "/etc/dhcp" ]; then
echo $DIR
fi
break
done









share|improve this question
























  • I edited your post and added some indentation. Feel free to revert it if you don't like it. But to my mind it is now clearer that in the second case the break is outside the if.
    – PerlDuck
    Dec 16 '18 at 11:43








  • 2




    @PerlDuck and that's why indentation is important. :P
    – Kulfy
    Dec 16 '18 at 12:38










  • What were the different outputs? And what were the inputs? Ah well, I suppose it doesn't really matter, maybe the title could be better
    – Xen2050
    Dec 17 '18 at 15:32














-1












-1








-1







I wrote a simple script:



for DIR in /etc/*; do
if [ "$DIR" == "/etc/dhcp" ]; then
echo $DIR
break
fi
done


This works correctly but if I use break after fi it doesn't generate the desirable result.



What is the difference between that script and this one?



for DIR in /etc/*; do
if [ "$DIR" == "/etc/dhcp" ]; then
echo $DIR
fi
break
done









share|improve this question















I wrote a simple script:



for DIR in /etc/*; do
if [ "$DIR" == "/etc/dhcp" ]; then
echo $DIR
break
fi
done


This works correctly but if I use break after fi it doesn't generate the desirable result.



What is the difference between that script and this one?



for DIR in /etc/*; do
if [ "$DIR" == "/etc/dhcp" ]; then
echo $DIR
fi
break
done






command-line bash scripts






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 16 '18 at 20:49









wjandrea

8,47842259




8,47842259










asked Dec 16 '18 at 7:36









Younes Nikkhah

6




6












  • I edited your post and added some indentation. Feel free to revert it if you don't like it. But to my mind it is now clearer that in the second case the break is outside the if.
    – PerlDuck
    Dec 16 '18 at 11:43








  • 2




    @PerlDuck and that's why indentation is important. :P
    – Kulfy
    Dec 16 '18 at 12:38










  • What were the different outputs? And what were the inputs? Ah well, I suppose it doesn't really matter, maybe the title could be better
    – Xen2050
    Dec 17 '18 at 15:32


















  • I edited your post and added some indentation. Feel free to revert it if you don't like it. But to my mind it is now clearer that in the second case the break is outside the if.
    – PerlDuck
    Dec 16 '18 at 11:43








  • 2




    @PerlDuck and that's why indentation is important. :P
    – Kulfy
    Dec 16 '18 at 12:38










  • What were the different outputs? And what were the inputs? Ah well, I suppose it doesn't really matter, maybe the title could be better
    – Xen2050
    Dec 17 '18 at 15:32
















I edited your post and added some indentation. Feel free to revert it if you don't like it. But to my mind it is now clearer that in the second case the break is outside the if.
– PerlDuck
Dec 16 '18 at 11:43






I edited your post and added some indentation. Feel free to revert it if you don't like it. But to my mind it is now clearer that in the second case the break is outside the if.
– PerlDuck
Dec 16 '18 at 11:43






2




2




@PerlDuck and that's why indentation is important. :P
– Kulfy
Dec 16 '18 at 12:38




@PerlDuck and that's why indentation is important. :P
– Kulfy
Dec 16 '18 at 12:38












What were the different outputs? And what were the inputs? Ah well, I suppose it doesn't really matter, maybe the title could be better
– Xen2050
Dec 17 '18 at 15:32




What were the different outputs? And what were the inputs? Ah well, I suppose it doesn't really matter, maybe the title could be better
– Xen2050
Dec 17 '18 at 15:32










1 Answer
1






active

oldest

votes


















6














In simple terms, if the condition is met, the for loop will be terminated in the first script. But in the second script, the for loop will terminate irrespective of whether the condition is met or not. That for loop will be executed once. So, there is no benefit of a for loop in the second script since the results would be the same.



So, at the end of the first script, the value of DIR will be /etc/dhcp and an output will be there. But in the case of the second script, the value of DIR will be the name of the folder found at first. It may happen that it is /etc/dhcp. In that case the if condition will be executed and you'll see an output. But if the value of DIR is something else, then there will be no output.






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%2f1102253%2fdifferent-outputs-when-using-break-in-bash%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









    6














    In simple terms, if the condition is met, the for loop will be terminated in the first script. But in the second script, the for loop will terminate irrespective of whether the condition is met or not. That for loop will be executed once. So, there is no benefit of a for loop in the second script since the results would be the same.



    So, at the end of the first script, the value of DIR will be /etc/dhcp and an output will be there. But in the case of the second script, the value of DIR will be the name of the folder found at first. It may happen that it is /etc/dhcp. In that case the if condition will be executed and you'll see an output. But if the value of DIR is something else, then there will be no output.






    share|improve this answer




























      6














      In simple terms, if the condition is met, the for loop will be terminated in the first script. But in the second script, the for loop will terminate irrespective of whether the condition is met or not. That for loop will be executed once. So, there is no benefit of a for loop in the second script since the results would be the same.



      So, at the end of the first script, the value of DIR will be /etc/dhcp and an output will be there. But in the case of the second script, the value of DIR will be the name of the folder found at first. It may happen that it is /etc/dhcp. In that case the if condition will be executed and you'll see an output. But if the value of DIR is something else, then there will be no output.






      share|improve this answer


























        6












        6








        6






        In simple terms, if the condition is met, the for loop will be terminated in the first script. But in the second script, the for loop will terminate irrespective of whether the condition is met or not. That for loop will be executed once. So, there is no benefit of a for loop in the second script since the results would be the same.



        So, at the end of the first script, the value of DIR will be /etc/dhcp and an output will be there. But in the case of the second script, the value of DIR will be the name of the folder found at first. It may happen that it is /etc/dhcp. In that case the if condition will be executed and you'll see an output. But if the value of DIR is something else, then there will be no output.






        share|improve this answer














        In simple terms, if the condition is met, the for loop will be terminated in the first script. But in the second script, the for loop will terminate irrespective of whether the condition is met or not. That for loop will be executed once. So, there is no benefit of a for loop in the second script since the results would be the same.



        So, at the end of the first script, the value of DIR will be /etc/dhcp and an output will be there. But in the case of the second script, the value of DIR will be the name of the folder found at first. It may happen that it is /etc/dhcp. In that case the if condition will be executed and you'll see an output. But if the value of DIR is something else, then there will be no output.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 16 '18 at 20:53









        wjandrea

        8,47842259




        8,47842259










        answered Dec 16 '18 at 7:44









        Kulfy

        3,58341139




        3,58341139






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f1102253%2fdifferent-outputs-when-using-break-in-bash%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

            Quarter-circle Tiles

            build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

            Mont Emei