Shell script while read loop executes only once Shell script while read loop executes only once shell shell

Shell script while read loop executes only once


Taken from this answer: I now echo nothing into HandbrakeCLI to ensure it's not using the same stdin as my script:

find . -name "*.mkv" | while read FILEdo    echo "" | handbrake-touch "$FILE"    if [ $? != 0 ]    then        echo "$FILE had problems" >> errors.log      fidone

...and it works as intended/expected.


One possibility is to use the safe find:

while IFS= read -r -d '' -u 9do    ~/Unix/HandbrakeCLI --input "$REPLY" --output "$REPLY".mp4 --preset="Normal"    touch -r "$REPLY" "$REPLY".mp4done 9< <( find "$@" -type f -print0 )

This should be POSIX compatible, but only works if neither HandbrakeCLI nor touch read from standard input and no file names contain newlines:

find "$@" -type f -print0 | while IFS= read -rdo    ~/Unix/HandbrakeCLI --input "$REPLY" --output "$REPLY".mp4 --preset="Normal"    touch -r "$REPLY" "$REPLY".mp4done


You are probably running with a shellopt '-e' (exit on errors)

Try

set +e