0

我有一段令人沮丧的时光。我编写了一个脚本,它应该逐行读取文件,并根据该行进行操作。具体来说,我正在使用 handbrake CLI 将部分目录(基于文件列表)转换为另一种更小的格式。我已经使用命令行的“echo”测试了脚本以空运行它,但是当我想实际运行脚本时,它只对列表中的第一项运行命令,然后退出。这是代码:

#!/bin/bash

if [ "$1" != "" ]; then
        SRC="$1"
else
        echo "No File List Specified"
        exit
fi
if [ "$2" != "" ]; then
        DEST="$2"
else
        echo "No Destination Path Specified"
        exit
fi

DEST_EXT=mp4
HANDBRAKE_CLI=HandBrakeCLI
profile="Very Fast 720p"

while read -r FILE
do
     echo "Next file is $FILE"
     if [ -f "$FILE" ]; then
        filename=$(basename "$FILE")
        extension=${filename##*.}
        newfilename=${filename%.*}
        newfullname="$newfilename.$DEST_EXT"
        if [ ! -f "$newfilename.$DEST_EXT" ]; then
                HandBrakeCLI -v --preset-import-gui "/home/master/Very Fast 720p.json" -Z "Very Fast 720p" -i "$FILE" -o "$DEST"/"$newfullname"
        fi
     fi
     echo "Moving to Next File"
done < "$SRC"

这是文件列表:

/mnt/raid/public/Movies/The Lost World - Jurassic Park 2.mkv
/mnt/raid/public/Movies/Jurassic Park.mkv
/mnt/raid/public/Movies/Frozen 2 (2019) - 1080p.mkv
/mnt/raid/public/Movies/ALITA - BATTLE ANGEL (2019) - 1080p.mkv
/mnt/raid/public/Movies/How To Train Your Dragon 3 - 1080p.mkv
/mnt/raid/public/Movies/Star Wars Ep VI - Return of the Jedi (1983) - 4K83.mkv
/mnt/raid/public/Movies/Star Wars (1977) - 4K77.mkv
/mnt/raid/public/Movies/Star Wars (1977) - Despecialized.mkv
/mnt/raid/public/Movies/Star Wars Ep VI - Return of the Jedi (1983) - Despecialized.mkv
/mnt/raid/public/Movies/The Empire Strikes Back (1980) - Despecialized.mkv
/mnt/raid/public/Movies/Scary Stories to Tell in the Dark.mkv
/mnt/raid/public/Movies/National Lampoon's Christmas Vacation 1989.mkv
/mnt/raid/public/Movies/How To Train Your Dragon 3.mkv
/mnt/raid/public/Movies/Logan.mkv
/mnt/raid/public/Movies/BRAM STOKER'S DRACULA.mkv
/mnt/raid/public/Movies/The Crow.mkv
/mnt/raid/public/Movies/Jurassic World - Fallen Kingdom.mkv
/mnt/raid/public/Movies/TOP GUN.mkv
/mnt/raid/public/Movies/PREDATOR.mkv
/mnt/raid/public/Movies/LOGANS RUN.mkv
/mnt/raid/public/Movies/V for Vendetta.mkv
/mnt/raid/public/Movies/Happiness is a Warm Blanket Charlie Brown.mkv
/mnt/raid/public/Movies/The Neverending Story.mkv

这是我将“echo”放在命令前面时的输出:

Next file is /mnt/raid/public/Movies/Jurassic Park 3.mkv
HandBrakeCLI -v --preset-import-gui /home/master/Very Fast 720p.json -Z Very Fast 720p -i /mnt/raid/public/Movies/Jurassic Park 3.mkv -o /mnt/raid/public/Movies/CarVideo/Jurassic Park 3.mp4
Moving to Next File
Next file is /mnt/raid/public/Movies/The Lost World - Jurassic Park 2.mkv
HandBrakeCLI -v --preset-import-gui /home/master/Very Fast 720p.json -Z Very Fast 720p -i /mnt/raid/public/Movies/The Lost World - Jurassic Park 2.mkv -o /mnt/raid/public/Movies/CarVideo/The Lost World - Jurassic Park 2.mp4
Moving to Next File
Next file is /mnt/raid/public/Movies/Jurassic Park.mkv
etc

但是当我删除回显和所需的引号时,手刹只在第一个文件上执行,然后整个脚本终止。我不明白这是怎么发生的,因为空运行在通过 do 循环时会按顺序吐出正确的命令。我尝试在 Handbrake CLI 命令之后等待,当我中断(编码期间的 ctl-c)脚本时,这似乎有所帮助,但是让编码一直进行会导致相同的行为(脚本终止)。

有没有人有任何想法?

4

2 回答 2

0

看起来像戈登戴维森的建议

Try sending the list over FD #3 instead of stdin with while read -r FILE <&3 and done 3< "$SRC". See BashFAQ #89. – Gordon Davisson 10 hours ago

解决了这个问题。脚本已经运行了一整夜。谢谢您的帮助。

于 2020-06-26T12:07:12.870 回答
-1

由于您的文件名包含空格,请尝试将您的字符串格式更改为:

filename="$(basename \"$FILE\")"
extension="${filename##*.}"
newfilename="${filename%.*}"
newfullname="$newfilename.$DEST_EXT"

我刚刚添加了双引号。我不确定这会解决你的问题,但我遇到了类似的问题,这有帮助。

于 2020-06-25T23:48:51.913 回答