bash 脚本,在 for 循环奇怪的行为中处理文件名中的空格
bash scripting, handling spaces in filesnames in a for loop strange behaviour
帮助。我尝试过的一切都失败了。我正在尝试修复我的视频 collection 并且很多视频在 names/etc 中都有空格 我想对它们进行转码并且我已经编写了下面的脚本,但它失败了。我用于测试的文件名中有 2 个是“13 Eerie (2013).avi”和 "Ace.Ventura.When.Nature.Calls.1995.720p.WEB-DL.x264-mSD.mkv"
我已经尝试了几种方法,从在查找时使用 print0 到破坏 IFS。任何援助将不胜感激。由于某些原因,当前版本将 e 字符上的所有内容分开
convert.sh
#!/bin/sh
OUTDIR="./done/"
LOGDIR="./logs/"
BACKUPDIR="./backup/"
# deal with spaces
SAVEIFS=$IFS
IFS=$(echo -en "\n")
# pull all files except MP4 or scripts in the CURRENT DIR only
for FULLFILENAME in `find . -maxdepth 1 -type f -not -iname "*.sh" -not -iname "*.mp4" -print`
do
# extract the file extension
filename=$(basename "$FULLFILENAME")
ext="${filename##*.}"
filename="${filename%.*}"
# use handbrake to convert to x264
HandBrakeCLI -i "$FULLFILENAME" -o "$OUTDIR$filename.mp4" -e x264 -q 22 -r 29.97 -B 64 -O 2>$LOGDIR$filename.log
# move the original file to a backup
mv "$FULLFILENAME" $BACKUPDIR
done
#restore the field separator
IFS=$SAVEIFS
- 不要使用
find
的命令替换
- 不要使用for循环
- 使用进程替换从
find
获取输出
- 使用
-print0
选项并使用while
循环阅读
- 最好避免大写变量名
- 使用
bash
shebang
代码:
#!/bin/bash
outdir="./done/"
logdir="./logs/"
backupdir="./backup/"
# pull all files except MP4 or scripts in the CURRENT DIR only
while IFS= read -r -d '' fullfilename
do
# extract the file extension
filename="$(basename "$fullfilename")"
ext="${filename##*.}"
filename="${filename%.*}"
# use handbrake to convert to x264
HandBrakeCLI -i "$fullfilename" -o "$outdir$filename.mp4" -e x264 -q 22 -r 29.97 -B 64 -O 2>"$logdir$filename.log"
# move the original file to a backup
mv "$fullfilename" "$backupdir"
done < <(find . -maxdepth 1 -type f -not -iname "*.sh" -not -iname "*.mp4" -print0)
帮助。我尝试过的一切都失败了。我正在尝试修复我的视频 collection 并且很多视频在 names/etc 中都有空格 我想对它们进行转码并且我已经编写了下面的脚本,但它失败了。我用于测试的文件名中有 2 个是“13 Eerie (2013).avi”和 "Ace.Ventura.When.Nature.Calls.1995.720p.WEB-DL.x264-mSD.mkv"
我已经尝试了几种方法,从在查找时使用 print0 到破坏 IFS。任何援助将不胜感激。由于某些原因,当前版本将 e 字符上的所有内容分开
convert.sh
#!/bin/sh
OUTDIR="./done/"
LOGDIR="./logs/"
BACKUPDIR="./backup/"
# deal with spaces
SAVEIFS=$IFS
IFS=$(echo -en "\n")
# pull all files except MP4 or scripts in the CURRENT DIR only
for FULLFILENAME in `find . -maxdepth 1 -type f -not -iname "*.sh" -not -iname "*.mp4" -print`
do
# extract the file extension
filename=$(basename "$FULLFILENAME")
ext="${filename##*.}"
filename="${filename%.*}"
# use handbrake to convert to x264
HandBrakeCLI -i "$FULLFILENAME" -o "$OUTDIR$filename.mp4" -e x264 -q 22 -r 29.97 -B 64 -O 2>$LOGDIR$filename.log
# move the original file to a backup
mv "$FULLFILENAME" $BACKUPDIR
done
#restore the field separator
IFS=$SAVEIFS
- 不要使用
find
的命令替换
- 不要使用for循环
- 使用进程替换从
find
获取输出
- 使用
-print0
选项并使用while
循环阅读 - 最好避免大写变量名
- 使用
bash
shebang
代码:
#!/bin/bash
outdir="./done/"
logdir="./logs/"
backupdir="./backup/"
# pull all files except MP4 or scripts in the CURRENT DIR only
while IFS= read -r -d '' fullfilename
do
# extract the file extension
filename="$(basename "$fullfilename")"
ext="${filename##*.}"
filename="${filename%.*}"
# use handbrake to convert to x264
HandBrakeCLI -i "$fullfilename" -o "$outdir$filename.mp4" -e x264 -q 22 -r 29.97 -B 64 -O 2>"$logdir$filename.log"
# move the original file to a backup
mv "$fullfilename" "$backupdir"
done < <(find . -maxdepth 1 -type f -not -iname "*.sh" -not -iname "*.mp4" -print0)