问题
I am having a frustrating time. I have written a script which should read a file line by line, and do an operation based on that line. Specifically, I'm using handbrake CLI to convert a partial directory (based on a file list) to another smaller format. I've tested the script using "echo"s for the command line to dry run it, but when I want to actually run the script, it only runs the command on the first item in the list, and then exits. Here's the code:
#!/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"
Here's the file list:
/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
Here's the output when I put "echo" in front of the command:
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
But when I remove the echo and required quotation marks, handbrake executes only on the first file, and then the whole script terminates. I don't understand how this could be happening, since the dry run is spitting out the correct commands sequentially as it moves through the do loop. I tried putting in a wait after the Handbrake CLI command, and that seemed to help when I interrupted (ctl-c during encode) the script, but letting an encode go all the way resulted in the same behavior (script terminating).
Does anyone have any ideas?
回答1:
It looks like Gordon Davisson suggestion
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
Solved the issue. Script has been running all night. THanks for the help.
回答2:
since your filenames contain spaces, try change your string formatting to:
filename="$(basename \"$FILE\")"
extension="${filename##*.}"
newfilename="${filename%.*}"
newfullname="$newfilename.$DEST_EXT"
I've just added double quotes. I'm not sure this will solve your problem but I've had a similar issue and this helped.
来源:https://stackoverflow.com/questions/62585875/bash-script-do-loop-exiting-early