我一直在开发我的第一个 Bash 脚本,它使用给定目录中的相应文件名生成
.nfo
文件。使用我在其他地方找到的代码,我已经能够实现这一目标,并且还在每个 .nfo
文件中包含以下文本:
<episodedetails>
<title>This is working successfully</title>
<episode>This bit however is not working</episode>
<season>This bit however is not working</season>
</episodedetails>
我正在努力解决的是
nfo
文件中的剧集和季节字段。
我的文件名格式是这样的: 圣经 - 以弗所书 (2009) - S01E01 (简介、背景、上帝的蓝图).vob
该特定
nfo
文件的输出(目前)是:
<episodedetails>
<title>Introduction, Background, God's Blueprint</title>
<episode>2009)</episode>
<season>The Book of Ephesians (2009)</season>
</episodedetails>
请您帮我调整我的代码,以便
nfo
变为:
<episodedetails>
<title>Introduction, Background, God's Blueprint</title>
<episode>01</episode>
<season>01</season>
</episodedetails>
具体出错的代码区域如下:
episode="${filename#* - *(}"
episode="${episode% - *.vob}"
season="${filename#* - }"
season="${season% - *.vob}"
完整代码:
#!/usr/bin/env bash
while IFS= read -rd '' file; do
filename="${file##*/}"
title="${filename%\).vob}"
title="${title##* (}"
episode="${filename#* - *(}"
episode="${episode% - *.vob}"
season="${filename#* - }"
season="${season% - *.vob}"
cat > "${file%\.vob}.nfo" <<< \
"<episodedetails>
<title>${title}</title>
<episode>${episode}</episode>
<season>${season}</season>
</episodedetails>"
done < <(find . -name '*.vob' -print0)
仅通过一个示例,很难知道什么在一般情况下有效。但我会关注 S##E## 令牌的非常具体的格式。
temp=${filename#*s[0-9][0-9]e[0-9][0-9]}
sxxexx=${filename%"$temp"}
season=${sxxexx%e[0-9][0-9]*}
season=${season#s}
episode=${sxxexx#s"$season"e}
episode=${episode%%\ *}
使用参数扩展来完成工作有点麻烦,但能够避免调用外部进程应该会保持相当快的速度。