Request for input on bash script loop











up vote
0
down vote

favorite












I am trying to run jpegoptim against pictures and at some point in the loop...after let's say 200 iterations, i get "stdin", which requires input to go further.



Is there a way to force the input?



#!/bin/bash
for i in `find . -name "*.jpg" -type f`; do
jpegoptim "$i" >> jpg.log; done
done









share|improve this question


























    up vote
    0
    down vote

    favorite












    I am trying to run jpegoptim against pictures and at some point in the loop...after let's say 200 iterations, i get "stdin", which requires input to go further.



    Is there a way to force the input?



    #!/bin/bash
    for i in `find . -name "*.jpg" -type f`; do
    jpegoptim "$i" >> jpg.log; done
    done









    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am trying to run jpegoptim against pictures and at some point in the loop...after let's say 200 iterations, i get "stdin", which requires input to go further.



      Is there a way to force the input?



      #!/bin/bash
      for i in `find . -name "*.jpg" -type f`; do
      jpegoptim "$i" >> jpg.log; done
      done









      share|improve this question













      I am trying to run jpegoptim against pictures and at some point in the loop...after let's say 200 iterations, i get "stdin", which requires input to go further.



      Is there a way to force the input?



      #!/bin/bash
      for i in `find . -name "*.jpg" -type f`; do
      jpegoptim "$i" >> jpg.log; done
      done






      bash jpegoptim






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 at 14:15









      Potney Switters

      1,01031939




      1,01031939
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          First, don't process dynamically generated file lists in a for loop.



          Second, your code is broken - you have 2 done's.



          Third, my apologies, not familiar with the tool, but I'm inclined to think your issue is in a broader piece of the code than presented.



          Fourth, (getting minor and nitpicky :) prefer $(...) over backticks. There is very rarely a reason not to do so in bash.



          Still, almost none of that is relevant to your question, aside from maybe the third thing... See if this helps -



          find . -name "*.jpg" -type f |
          while read -r file; do jpegoptim "$file"; done >> jpg.log 2>&1


          Or possibly better,



          find . -name "*.jpg" -type f | xargs jpegoptim  >> jpg.log 2>&1



          Update




          Benjamin W. points out that these break when filenames have embedded newlines, which is entirely true. I consider filenames with embedded newlines a heinous heresy of the highest order, nut sometimes you don't have control of that, so per his entirely valid suggestion:



          find . -name "*.jpg" -type f |
          while read -r -d '' file; do jpegoptim "$file"; done >> jpg.log 2>&1


          or



          find . -name "*.jpg" -type f -print0 | xargs -0 jpegoptim  >> jpg.log 2>&1


          or best, for simplicity and (therefore) safety



          find . -name "*.jpg" -type f -exec jpegoptim {} ; >> jpg.log 2>&1


          (Please check my syntax on those...)



          Though there are still efficiency considerations if you are processing a large number of files. One should also consider the possibility that the target program may or may not be able to multiprocess command-line arguments. Consider the difference between these:



          $: find /tmp -type f -exec echo {} ;
          /tmp/.mintty-version
          /tmp/AdobeARM.log
          /tmp/foo
          . . .

          $: find /tmp -type f | xargs echo
          /tmp/.mintty-version /tmp/AdobeARM.log /tmp/foo ...





          share|improve this answer



















          • 1




            Both of these break for files with blanks or newlines in their names. Best practice is to use -print0 | xargs -0, or -print0 | while read -r -d ''. Or just use find -exec instead of either.
            – Benjamin W.
            Nov 19 at 14:41




















          up vote
          2
          down vote













          You can use a counter then ask for user input using read when the counter reaches 200



          #!/bin/bash

          count=0
          for i in `find . -name "*.jpg" -type f`; do
          jpegoptim "$i" >> jpg.log
          [[ $((count++)) == 200 ]] && read -rp "Continue? [y/n] " resume
          [[ "$resume" == 'n' ]] && break
          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',
            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%2f53376521%2frequest-for-input-on-bash-script-loop%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            First, don't process dynamically generated file lists in a for loop.



            Second, your code is broken - you have 2 done's.



            Third, my apologies, not familiar with the tool, but I'm inclined to think your issue is in a broader piece of the code than presented.



            Fourth, (getting minor and nitpicky :) prefer $(...) over backticks. There is very rarely a reason not to do so in bash.



            Still, almost none of that is relevant to your question, aside from maybe the third thing... See if this helps -



            find . -name "*.jpg" -type f |
            while read -r file; do jpegoptim "$file"; done >> jpg.log 2>&1


            Or possibly better,



            find . -name "*.jpg" -type f | xargs jpegoptim  >> jpg.log 2>&1



            Update




            Benjamin W. points out that these break when filenames have embedded newlines, which is entirely true. I consider filenames with embedded newlines a heinous heresy of the highest order, nut sometimes you don't have control of that, so per his entirely valid suggestion:



            find . -name "*.jpg" -type f |
            while read -r -d '' file; do jpegoptim "$file"; done >> jpg.log 2>&1


            or



            find . -name "*.jpg" -type f -print0 | xargs -0 jpegoptim  >> jpg.log 2>&1


            or best, for simplicity and (therefore) safety



            find . -name "*.jpg" -type f -exec jpegoptim {} ; >> jpg.log 2>&1


            (Please check my syntax on those...)



            Though there are still efficiency considerations if you are processing a large number of files. One should also consider the possibility that the target program may or may not be able to multiprocess command-line arguments. Consider the difference between these:



            $: find /tmp -type f -exec echo {} ;
            /tmp/.mintty-version
            /tmp/AdobeARM.log
            /tmp/foo
            . . .

            $: find /tmp -type f | xargs echo
            /tmp/.mintty-version /tmp/AdobeARM.log /tmp/foo ...





            share|improve this answer



















            • 1




              Both of these break for files with blanks or newlines in their names. Best practice is to use -print0 | xargs -0, or -print0 | while read -r -d ''. Or just use find -exec instead of either.
              – Benjamin W.
              Nov 19 at 14:41

















            up vote
            1
            down vote



            accepted










            First, don't process dynamically generated file lists in a for loop.



            Second, your code is broken - you have 2 done's.



            Third, my apologies, not familiar with the tool, but I'm inclined to think your issue is in a broader piece of the code than presented.



            Fourth, (getting minor and nitpicky :) prefer $(...) over backticks. There is very rarely a reason not to do so in bash.



            Still, almost none of that is relevant to your question, aside from maybe the third thing... See if this helps -



            find . -name "*.jpg" -type f |
            while read -r file; do jpegoptim "$file"; done >> jpg.log 2>&1


            Or possibly better,



            find . -name "*.jpg" -type f | xargs jpegoptim  >> jpg.log 2>&1



            Update




            Benjamin W. points out that these break when filenames have embedded newlines, which is entirely true. I consider filenames with embedded newlines a heinous heresy of the highest order, nut sometimes you don't have control of that, so per his entirely valid suggestion:



            find . -name "*.jpg" -type f |
            while read -r -d '' file; do jpegoptim "$file"; done >> jpg.log 2>&1


            or



            find . -name "*.jpg" -type f -print0 | xargs -0 jpegoptim  >> jpg.log 2>&1


            or best, for simplicity and (therefore) safety



            find . -name "*.jpg" -type f -exec jpegoptim {} ; >> jpg.log 2>&1


            (Please check my syntax on those...)



            Though there are still efficiency considerations if you are processing a large number of files. One should also consider the possibility that the target program may or may not be able to multiprocess command-line arguments. Consider the difference between these:



            $: find /tmp -type f -exec echo {} ;
            /tmp/.mintty-version
            /tmp/AdobeARM.log
            /tmp/foo
            . . .

            $: find /tmp -type f | xargs echo
            /tmp/.mintty-version /tmp/AdobeARM.log /tmp/foo ...





            share|improve this answer



















            • 1




              Both of these break for files with blanks or newlines in their names. Best practice is to use -print0 | xargs -0, or -print0 | while read -r -d ''. Or just use find -exec instead of either.
              – Benjamin W.
              Nov 19 at 14:41















            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted






            First, don't process dynamically generated file lists in a for loop.



            Second, your code is broken - you have 2 done's.



            Third, my apologies, not familiar with the tool, but I'm inclined to think your issue is in a broader piece of the code than presented.



            Fourth, (getting minor and nitpicky :) prefer $(...) over backticks. There is very rarely a reason not to do so in bash.



            Still, almost none of that is relevant to your question, aside from maybe the third thing... See if this helps -



            find . -name "*.jpg" -type f |
            while read -r file; do jpegoptim "$file"; done >> jpg.log 2>&1


            Or possibly better,



            find . -name "*.jpg" -type f | xargs jpegoptim  >> jpg.log 2>&1



            Update




            Benjamin W. points out that these break when filenames have embedded newlines, which is entirely true. I consider filenames with embedded newlines a heinous heresy of the highest order, nut sometimes you don't have control of that, so per his entirely valid suggestion:



            find . -name "*.jpg" -type f |
            while read -r -d '' file; do jpegoptim "$file"; done >> jpg.log 2>&1


            or



            find . -name "*.jpg" -type f -print0 | xargs -0 jpegoptim  >> jpg.log 2>&1


            or best, for simplicity and (therefore) safety



            find . -name "*.jpg" -type f -exec jpegoptim {} ; >> jpg.log 2>&1


            (Please check my syntax on those...)



            Though there are still efficiency considerations if you are processing a large number of files. One should also consider the possibility that the target program may or may not be able to multiprocess command-line arguments. Consider the difference between these:



            $: find /tmp -type f -exec echo {} ;
            /tmp/.mintty-version
            /tmp/AdobeARM.log
            /tmp/foo
            . . .

            $: find /tmp -type f | xargs echo
            /tmp/.mintty-version /tmp/AdobeARM.log /tmp/foo ...





            share|improve this answer














            First, don't process dynamically generated file lists in a for loop.



            Second, your code is broken - you have 2 done's.



            Third, my apologies, not familiar with the tool, but I'm inclined to think your issue is in a broader piece of the code than presented.



            Fourth, (getting minor and nitpicky :) prefer $(...) over backticks. There is very rarely a reason not to do so in bash.



            Still, almost none of that is relevant to your question, aside from maybe the third thing... See if this helps -



            find . -name "*.jpg" -type f |
            while read -r file; do jpegoptim "$file"; done >> jpg.log 2>&1


            Or possibly better,



            find . -name "*.jpg" -type f | xargs jpegoptim  >> jpg.log 2>&1



            Update




            Benjamin W. points out that these break when filenames have embedded newlines, which is entirely true. I consider filenames with embedded newlines a heinous heresy of the highest order, nut sometimes you don't have control of that, so per his entirely valid suggestion:



            find . -name "*.jpg" -type f |
            while read -r -d '' file; do jpegoptim "$file"; done >> jpg.log 2>&1


            or



            find . -name "*.jpg" -type f -print0 | xargs -0 jpegoptim  >> jpg.log 2>&1


            or best, for simplicity and (therefore) safety



            find . -name "*.jpg" -type f -exec jpegoptim {} ; >> jpg.log 2>&1


            (Please check my syntax on those...)



            Though there are still efficiency considerations if you are processing a large number of files. One should also consider the possibility that the target program may or may not be able to multiprocess command-line arguments. Consider the difference between these:



            $: find /tmp -type f -exec echo {} ;
            /tmp/.mintty-version
            /tmp/AdobeARM.log
            /tmp/foo
            . . .

            $: find /tmp -type f | xargs echo
            /tmp/.mintty-version /tmp/AdobeARM.log /tmp/foo ...






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 19 at 15:06

























            answered Nov 19 at 14:38









            Paul Hodges

            2,1591320




            2,1591320








            • 1




              Both of these break for files with blanks or newlines in their names. Best practice is to use -print0 | xargs -0, or -print0 | while read -r -d ''. Or just use find -exec instead of either.
              – Benjamin W.
              Nov 19 at 14:41
















            • 1




              Both of these break for files with blanks or newlines in their names. Best practice is to use -print0 | xargs -0, or -print0 | while read -r -d ''. Or just use find -exec instead of either.
              – Benjamin W.
              Nov 19 at 14:41










            1




            1




            Both of these break for files with blanks or newlines in their names. Best practice is to use -print0 | xargs -0, or -print0 | while read -r -d ''. Or just use find -exec instead of either.
            – Benjamin W.
            Nov 19 at 14:41






            Both of these break for files with blanks or newlines in their names. Best practice is to use -print0 | xargs -0, or -print0 | while read -r -d ''. Or just use find -exec instead of either.
            – Benjamin W.
            Nov 19 at 14:41














            up vote
            2
            down vote













            You can use a counter then ask for user input using read when the counter reaches 200



            #!/bin/bash

            count=0
            for i in `find . -name "*.jpg" -type f`; do
            jpegoptim "$i" >> jpg.log
            [[ $((count++)) == 200 ]] && read -rp "Continue? [y/n] " resume
            [[ "$resume" == 'n' ]] && break
            done





            share|improve this answer



























              up vote
              2
              down vote













              You can use a counter then ask for user input using read when the counter reaches 200



              #!/bin/bash

              count=0
              for i in `find . -name "*.jpg" -type f`; do
              jpegoptim "$i" >> jpg.log
              [[ $((count++)) == 200 ]] && read -rp "Continue? [y/n] " resume
              [[ "$resume" == 'n' ]] && break
              done





              share|improve this answer

























                up vote
                2
                down vote










                up vote
                2
                down vote









                You can use a counter then ask for user input using read when the counter reaches 200



                #!/bin/bash

                count=0
                for i in `find . -name "*.jpg" -type f`; do
                jpegoptim "$i" >> jpg.log
                [[ $((count++)) == 200 ]] && read -rp "Continue? [y/n] " resume
                [[ "$resume" == 'n' ]] && break
                done





                share|improve this answer














                You can use a counter then ask for user input using read when the counter reaches 200



                #!/bin/bash

                count=0
                for i in `find . -name "*.jpg" -type f`; do
                jpegoptim "$i" >> jpg.log
                [[ $((count++)) == 200 ]] && read -rp "Continue? [y/n] " resume
                [[ "$resume" == 'n' ]] && break
                done






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 19 at 14:43

























                answered Nov 19 at 14:35









                ssemilla

                2,457421




                2,457421






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376521%2frequest-for-input-on-bash-script-loop%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

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    TypeError: fit_transform() missing 1 required positional argument: 'X'