Prevent the child process (HandbrakeCLI) from turning off the parent script

I have a batch conversion script to convert .mkvs of various sizes to ipmp / iphone.mp4s size, cropping / scaling as required. Determining the original size, the required trim, the output file works fine. However, upon successful completion of the first conversion, the HandbrakeCLI leads to the exit of the parent script. Why should it be? And how can I stop him?

The code that currently stands for:

#!/bin/bash find . -name "*.mkv" | while read FILE do # What would the output file be? DST=../Touch/$(dirname "$FILE") MKV=$(basename "$FILE") MP4=${MKV%%.mkv}.mp4 # If it already exists, don't overwrite it if [ -e "$DST/$MP4" ] then echo "NOT overwriting $DST/$MP4" else # Stuff to determine dimensions/cropping removed for brevity HandbrakeCLI --preset "iPhone & iPod Touch" --vb 900 --crop $crop -i "$FILE" -o "$DST/$MP4" > /dev/null 2>&1 if [ $? != 0 ] then echo "$FILE had problems" >> errors.log fi fi done 

I additionally tried it with a trap, but this did not change the behavior (although the last trap worked)

 trap "echo Handbrake SIGINT-d" SIGINT trap "echo Handbrake SIGTERM-d" SIGTERM trap "echo Handbrake EXIT-d" EXIT trap "echo Handbrake 0-d" 0 

Edited to add:

The fact that the '0' trap fired prompted me to investigate why this might be. Executing as bash -x $script showed that the find | while read find | while read ends prematurely.

I reanalyzed the search and coding into separate scripts. Now the search loop:

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

The behavior remains unchanged - one code followed by the end of the while loop. If I just put "echo $ FILE" instead of "handbrake-touch", all the files are listed. The current directory does not change (I was wondering what find | while might break).

+4
source share
1 answer

Now allowed, thanks to the prompt of this other thread. I now don’t give anything back to HandbrakeCLI to make sure it is 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 works as intended / expected.

+6
source

All Articles