Call of HandBrakeCLI within script breaks loop

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












2















In my bash script I have a while loop as follows. What I don't understand is, why the call of HandBrakeCLI breaks the loop.
If the 'if' condition is false and 'else' is follwed, the loop continues. Here is my script:



FILES="xxx*.ts
yyy.ts"

for f in $FILES
do
find /pathto -name $f -print | while IFS= read -r file; do
echo "file found = $file"
outputfile=$outputpath$(basename "$file")".mp4"

if [[ ! -f $outputfile ]] then
HandBrakeCLI -i "$file" -o "$outputfile" -e x264 -q 22 -r 15 -B 64 -X 480 -O
else echo "outputfile already exists: $outputfile"
fi
done
done


So I have three files, named xxx1.ts, xxx2.ts and yyy.ts.
All should be found and converted by HandBrakeCLI. But after the first conversion (of xxx1.ts) the while loop breaks and yyy.ts is processed.
If I start the script again, it finds the first file being already converted and enters the else branch. This time the while loop doesn't break and it processes xxx2.ts and then yyy.ts



Why does the HandBrakeCLI call breaks my loop and how can I prevent it?










share|improve this question
























  • Typo: [[ ... ]] then should be [[ ... ]]; then. Also, you'd be better off with FILES as an array, or you'd be matching filenames in the current directory with those patterns. And double quote $f. Consider using shellcheck.net

    – Kusalananda
    Jan 27 at 18:47
















2















In my bash script I have a while loop as follows. What I don't understand is, why the call of HandBrakeCLI breaks the loop.
If the 'if' condition is false and 'else' is follwed, the loop continues. Here is my script:



FILES="xxx*.ts
yyy.ts"

for f in $FILES
do
find /pathto -name $f -print | while IFS= read -r file; do
echo "file found = $file"
outputfile=$outputpath$(basename "$file")".mp4"

if [[ ! -f $outputfile ]] then
HandBrakeCLI -i "$file" -o "$outputfile" -e x264 -q 22 -r 15 -B 64 -X 480 -O
else echo "outputfile already exists: $outputfile"
fi
done
done


So I have three files, named xxx1.ts, xxx2.ts and yyy.ts.
All should be found and converted by HandBrakeCLI. But after the first conversion (of xxx1.ts) the while loop breaks and yyy.ts is processed.
If I start the script again, it finds the first file being already converted and enters the else branch. This time the while loop doesn't break and it processes xxx2.ts and then yyy.ts



Why does the HandBrakeCLI call breaks my loop and how can I prevent it?










share|improve this question
























  • Typo: [[ ... ]] then should be [[ ... ]]; then. Also, you'd be better off with FILES as an array, or you'd be matching filenames in the current directory with those patterns. And double quote $f. Consider using shellcheck.net

    – Kusalananda
    Jan 27 at 18:47














2












2








2








In my bash script I have a while loop as follows. What I don't understand is, why the call of HandBrakeCLI breaks the loop.
If the 'if' condition is false and 'else' is follwed, the loop continues. Here is my script:



FILES="xxx*.ts
yyy.ts"

for f in $FILES
do
find /pathto -name $f -print | while IFS= read -r file; do
echo "file found = $file"
outputfile=$outputpath$(basename "$file")".mp4"

if [[ ! -f $outputfile ]] then
HandBrakeCLI -i "$file" -o "$outputfile" -e x264 -q 22 -r 15 -B 64 -X 480 -O
else echo "outputfile already exists: $outputfile"
fi
done
done


So I have three files, named xxx1.ts, xxx2.ts and yyy.ts.
All should be found and converted by HandBrakeCLI. But after the first conversion (of xxx1.ts) the while loop breaks and yyy.ts is processed.
If I start the script again, it finds the first file being already converted and enters the else branch. This time the while loop doesn't break and it processes xxx2.ts and then yyy.ts



Why does the HandBrakeCLI call breaks my loop and how can I prevent it?










share|improve this question
















In my bash script I have a while loop as follows. What I don't understand is, why the call of HandBrakeCLI breaks the loop.
If the 'if' condition is false and 'else' is follwed, the loop continues. Here is my script:



FILES="xxx*.ts
yyy.ts"

for f in $FILES
do
find /pathto -name $f -print | while IFS= read -r file; do
echo "file found = $file"
outputfile=$outputpath$(basename "$file")".mp4"

if [[ ! -f $outputfile ]] then
HandBrakeCLI -i "$file" -o "$outputfile" -e x264 -q 22 -r 15 -B 64 -X 480 -O
else echo "outputfile already exists: $outputfile"
fi
done
done


So I have three files, named xxx1.ts, xxx2.ts and yyy.ts.
All should be found and converted by HandBrakeCLI. But after the first conversion (of xxx1.ts) the while loop breaks and yyy.ts is processed.
If I start the script again, it finds the first file being already converted and enters the else branch. This time the while loop doesn't break and it processes xxx2.ts and then yyy.ts



Why does the HandBrakeCLI call breaks my loop and how can I prevent it?







bash shell-script






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 27 at 18:48







Olaf

















asked Jan 27 at 18:44









OlafOlaf

112




112












  • Typo: [[ ... ]] then should be [[ ... ]]; then. Also, you'd be better off with FILES as an array, or you'd be matching filenames in the current directory with those patterns. And double quote $f. Consider using shellcheck.net

    – Kusalananda
    Jan 27 at 18:47


















  • Typo: [[ ... ]] then should be [[ ... ]]; then. Also, you'd be better off with FILES as an array, or you'd be matching filenames in the current directory with those patterns. And double quote $f. Consider using shellcheck.net

    – Kusalananda
    Jan 27 at 18:47

















Typo: [[ ... ]] then should be [[ ... ]]; then. Also, you'd be better off with FILES as an array, or you'd be matching filenames in the current directory with those patterns. And double quote $f. Consider using shellcheck.net

– Kusalananda
Jan 27 at 18:47






Typo: [[ ... ]] then should be [[ ... ]]; then. Also, you'd be better off with FILES as an array, or you'd be matching filenames in the current directory with those patterns. And double quote $f. Consider using shellcheck.net

– Kusalananda
Jan 27 at 18:47











1 Answer
1






active

oldest

votes


















0














while as written is not safe to use as standard input is here shared between processes so HandBrakeCLI is likely consuming standard input as well. This can be seen in a minimal example:



$ printf "anbncn" 
| while IFS= read -r x; do echo $x; sed 's/$/ sed was here/'; done
a
b sed was here
c sed was here


and thus the while loop exits early as there is nothing else to read from standard input after sed graffitied up the lines.



Workarounds are to hope and pray nothing else in the code is using some other file descriptor, say 3



while IFS= read -u 3 -r x; do
echo $x
sed 's/$/ sed read this line/'
done 3< <(printf "anbncn")


or to switch to a different programming language. (sed will now hang, unless you feed standard input to it, somehow.)



Another option would be to close standard input to your program, e.g.



HandBrakeCLI ... <&-


as that will prevent it from seeing the standard input the while loop needs.






share|improve this answer






















    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    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: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    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%2funix.stackexchange.com%2fquestions%2f497065%2fcall-of-handbrakecli-within-script-breaks-loop%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














    while as written is not safe to use as standard input is here shared between processes so HandBrakeCLI is likely consuming standard input as well. This can be seen in a minimal example:



    $ printf "anbncn" 
    | while IFS= read -r x; do echo $x; sed 's/$/ sed was here/'; done
    a
    b sed was here
    c sed was here


    and thus the while loop exits early as there is nothing else to read from standard input after sed graffitied up the lines.



    Workarounds are to hope and pray nothing else in the code is using some other file descriptor, say 3



    while IFS= read -u 3 -r x; do
    echo $x
    sed 's/$/ sed read this line/'
    done 3< <(printf "anbncn")


    or to switch to a different programming language. (sed will now hang, unless you feed standard input to it, somehow.)



    Another option would be to close standard input to your program, e.g.



    HandBrakeCLI ... <&-


    as that will prevent it from seeing the standard input the while loop needs.






    share|improve this answer



























      0














      while as written is not safe to use as standard input is here shared between processes so HandBrakeCLI is likely consuming standard input as well. This can be seen in a minimal example:



      $ printf "anbncn" 
      | while IFS= read -r x; do echo $x; sed 's/$/ sed was here/'; done
      a
      b sed was here
      c sed was here


      and thus the while loop exits early as there is nothing else to read from standard input after sed graffitied up the lines.



      Workarounds are to hope and pray nothing else in the code is using some other file descriptor, say 3



      while IFS= read -u 3 -r x; do
      echo $x
      sed 's/$/ sed read this line/'
      done 3< <(printf "anbncn")


      or to switch to a different programming language. (sed will now hang, unless you feed standard input to it, somehow.)



      Another option would be to close standard input to your program, e.g.



      HandBrakeCLI ... <&-


      as that will prevent it from seeing the standard input the while loop needs.






      share|improve this answer

























        0












        0








        0







        while as written is not safe to use as standard input is here shared between processes so HandBrakeCLI is likely consuming standard input as well. This can be seen in a minimal example:



        $ printf "anbncn" 
        | while IFS= read -r x; do echo $x; sed 's/$/ sed was here/'; done
        a
        b sed was here
        c sed was here


        and thus the while loop exits early as there is nothing else to read from standard input after sed graffitied up the lines.



        Workarounds are to hope and pray nothing else in the code is using some other file descriptor, say 3



        while IFS= read -u 3 -r x; do
        echo $x
        sed 's/$/ sed read this line/'
        done 3< <(printf "anbncn")


        or to switch to a different programming language. (sed will now hang, unless you feed standard input to it, somehow.)



        Another option would be to close standard input to your program, e.g.



        HandBrakeCLI ... <&-


        as that will prevent it from seeing the standard input the while loop needs.






        share|improve this answer













        while as written is not safe to use as standard input is here shared between processes so HandBrakeCLI is likely consuming standard input as well. This can be seen in a minimal example:



        $ printf "anbncn" 
        | while IFS= read -r x; do echo $x; sed 's/$/ sed was here/'; done
        a
        b sed was here
        c sed was here


        and thus the while loop exits early as there is nothing else to read from standard input after sed graffitied up the lines.



        Workarounds are to hope and pray nothing else in the code is using some other file descriptor, say 3



        while IFS= read -u 3 -r x; do
        echo $x
        sed 's/$/ sed read this line/'
        done 3< <(printf "anbncn")


        or to switch to a different programming language. (sed will now hang, unless you feed standard input to it, somehow.)



        Another option would be to close standard input to your program, e.g.



        HandBrakeCLI ... <&-


        as that will prevent it from seeing the standard input the while loop needs.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 27 at 19:22









        thrigthrig

        24.9k23157




        24.9k23157



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • 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%2funix.stackexchange.com%2fquestions%2f497065%2fcall-of-handbrakecli-within-script-breaks-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

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay