Note that there are some explanatory texts on larger screens.

plurals
  1. POShell script while read loop executes only once
    primarykey
    data
    text
    <p>I am writing a shell script to batch process .mov files off my camera through <a href="https://trac.handbrake.fr/wiki/CLIGuide" rel="nofollow noreferrer">Handbrake</a> to save HD space. The script searches a directory with 'find' and then runs Handbrake on each .mov file found, matching the creation date of the resulting file to the source file's date with 'touch'.</p> <p>I originally did this with a <strong>for</strong> loop:</p> <pre><code>for i in $(find "$*" -iname '*.mov') ; do ~/Unix/HandbrakeCLI --input "$i" --output "$i".mp4 --preset="Normal" touch -r "$i" "$i".mp4 done </code></pre> <p>This worked, but failed if the input files had spaces in their file names. So I tried a <strong>while</strong> loop instead:</p> <pre><code>find "$*" -iname '*.mov' | while read i ; do ~/Unix/HandbrakeCLI --input "$i" --output "$i".mp4 --preset="Normal" touch -r "$i" "$i".mp4 done </code></pre> <p>The problem with this loop is that it works for the first file in the directory, and then exits the loop. Note that if I substitute an "echo $i" in the body of the while loop, it prints all of the .mov files in the directory, so the loop is structured correctly.</p> <p>I believe there is a partial answer to my question on <a href="https://stackoverflow.com/questions/2627838/why-does-bash-loop-deploy-script-only-seem-to-work-once">this stackoverflow thread</a>. But the solution is specific to ssh and doesn't solve the general problem. Seems to have something do do with stdin being used by a sub-process, but I don't exactly understand how this works.</p> <p>Any advice?</p> <p>I'm on OSX 10.6</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload