$ P $从导致父脚本退出pventing一个子进程(HandbrakeCLI) [英] Preventing a child process (HandbrakeCLI) from causing the parent script to exit

查看:129
本文介绍了$ P $从导致父脚本退出pventing一个子进程(HandbrakeCLI)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批量转换脚本把不同尺寸的.mkvs到iPod / iPhone的尺寸.mp4s,裁剪/缩放以适应。确定原始尺寸,所需的作物,输出文件是所有工作的罚款。然而,在第一次转换成功完成,HandbrakeCLI导致父脚本退出。为什么会这样?我怎么能阻止它?

I have a batch conversion script to turn .mkvs of various dimensions into ipod/iphone sized .mp4s, cropping/scaling to suit. Determining the original dimensions, required crop, output file is all working fine. However, on successful completion of the first conversion, HandbrakeCLI causes the parent script to exit. Why would this be? And how can I stop it?

在code,因为它目前为:

The code, as it currently stands:

#!/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 have additionally tried it with a trap, but that didn't change the behaviour (although the last trap did fire)

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 $脚本透露,找到|而读循环pmaturely结束$ P $。

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

我已经重新分解查找和连接code到单独的脚本。这一发现循环是现在:

I've re-factored the find and encode into separate scripts. The find loop is now:

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

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

行为仍然是相同的 - 一个连接code后跟while循环的结局。如果我只是把一个回声$ FILE代替手刹触摸',所有的文件将列出。当前目录不会改变(我想知道什么可能打破找到|而)。

推荐答案

现在解决了,多亏了的这个其他线程。我现在没有什么呼应成HandbrakeCLI,以确保它不使用相同的标准输入我的脚本:

Now resolved, thanks to a clue from this other thread. 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.

这篇关于$ P $从导致父脚本退出pventing一个子进程(HandbrakeCLI)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆