bash – 防止子进程(HandbrakeCLI)导致父脚本退出

我有一个批量转换脚本将各种尺寸的.mkvs变成ipod / iphone大小.mp4s,裁剪/缩放以适应.确定原始尺寸,所需的裁剪,输出文件都工作正常.但是,成功完成第一次转换后,HandbrakeCLI会导致父脚本退出.为什么会这样?我怎么能阻止它?

目前的代码:

#!/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

我还用陷阱尝试了它,但这没有改变行为(虽然最后一个陷阱确实发生了火灾)

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

编辑添加:

“0”陷阱被解雇的事实促使我调查为什么会这样.执行bash -x $脚本显示找到|而read循环过早结束.

我重新考虑了查找并编码成单独的脚本. find循环现在是:

find . -name "*.mkv" | while read FILE
do
    handbrake-touch "$FILE"

    if [ $? != 0 ]
    then
        echo "$FILE had problems" >> errors.log  
    fi
done

行为保持不变 – 一个编码后跟while循环结束.如果我只是用’echo $FILE’代替’handbrake-touch’,那么列出所有文件.当前目录没有改变(我想知道什么可能会破坏find | while).

现在解决了,感谢 this其他线程的线索.我现在没有回应HandbrakeCLI,以确保它不使用与我的脚本相同的标准输入:
find . -name "*.mkv" | while read FILE
do
    echo "" | handbrake-touch "$FILE"

    if [ $? != 0 ]
    then
        echo "$FILE had problems" >> errors.log  
    fi
done

……它按预期/预期工作.

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...