I am writing a shell script to batch process .mov files off my camera through Handbrake 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'.
I originally did this with a for loop:
for i in $(find "$*" -iname '*.mov') ; do
~/Unix/HandbrakeCLI --input "$i" --output "$i".mp4 --preset="Normal"
touch -r "$i" "$i".mp4
done
This worked, but failed if the input files had spaces in their file names. So I tried a while loop instead:
find "$*" -iname '*.mov' | while read i ; do
~/Unix/HandbrakeCLI --input "$i" --output "$i".mp4 --preset="Normal"
touch -r "$i" "$i".mp4
done
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.
I believe there is a partial answer to my question on this stackoverflow thread. 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.
Any advice?
I'm on OSX 10.6
Shell scripts have access to some "magic" variables from the environment: $0 - The name of the script. $1 - The first argument sent to the script. $2 - The second argument sent to the script.
$? is the exit status of the most recently-executed command; by convention, 0 means success and anything else indicates failure. That line is testing whether the grep command succeeded. The grep manpage states: The exit status is 0 if selected lines are found, and 1 if not found.
$() – the command substitution. ${} – the parameter substitution/variable expansion.
Syntax for While loop In the above syntax example, until the condition evaluates to true all the commands command1, command2 between do and done keywords will be executed and while loop will terminate when the condition is not satisfied and proceeds to statement next to the done keyword.
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 FILE
do
echo "" | handbrake-touch "$FILE"
if [ $? != 0 ]
then
echo "$FILE had problems" >> errors.log
fi
done
...and it works as intended/expected.
One possibility is to use the safe find:
while IFS= read -r -d '' -u 9
do
~/Unix/HandbrakeCLI --input "$REPLY" --output "$REPLY".mp4 --preset="Normal"
touch -r "$REPLY" "$REPLY".mp4
done 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 -r
do
~/Unix/HandbrakeCLI --input "$REPLY" --output "$REPLY".mp4 --preset="Normal"
touch -r "$REPLY" "$REPLY".mp4
done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With