脚本无法重命名文件名中包含 08 或 09 的文件
Script failing in rename of files containing 08 or 09 in filename
我一直在尝试使用以下脚本来 link 我 iTunes 中的所有电视节目,例如:
- ./iTunes/iTunes Media/TV Shows/Rookie Blue/Season 5/01 Blink.m4v
- ./iTunes/iTunes Media/TV Shows/Haven/Season 3/11 最后的告别.m4v
- ./iTunes/iTunes Media/TV Shows/Doctor 谁 (2005)/第 5/10 季 Vincent and the Doctor.m4v
到新目录,格式为./TVShows/Show.Name/Season.nn/Show.Name.SnnEnn.Episode.Name.m4v
例如:
- ./TVShows/Rookie.Blue/Season.05/Rookie.Blue.S05E01.Blink.m4v
- ./TVShows/Haven/Season.03/Haven.S03E11.Last.Goodbyes.m4v
- ./TVShows/TV Shows/Doctor.Who.2005/Season.05/Doctor.Who.2005.S05E10.Vincent.and.the.Doctor.m4v
#! /bin/bash
srcroot="./iTunes/iTunes Media/TV Shows"
destroot="./TVShows"
for f in "$srcroot"/*/*/*.{m4v,mp4,avi}
do
[[ -f $f ]] || continue
IFS=/ read -a vid <<< "${f// /.}"
vid=( "${vid[@]: -3}" )
show=${vid[0]}
season=${vid[1]}
episode=${vid[2]}
[[ $show =~ \([0-9]{4}\)$ ]] && show=${show: 0:${#show}-6}${show: ${#show}-5:4}
sn=${season#*.}
IFS=. read ep eptitle <<< "${episode%.*}"
ext=${episode##*.}
printf -v hldir -- '%s/%s/%s' "$destroot" "$show" "$season"
printf -v hlname -- '%s.s%02de%02d.%s.%s' "$show" "$sn" "$ep" "$eptitle" "$ext"
echo "from: '$f'"
echo "to: '$hldir/$hlname'"
mkdir -p "$hldir"
ln "$f" "$hldir/$hlname"
echo
done
除了遇到第 08 或 09 集外,这个脚本似乎大部分都符合我的要求
例如:
- ./iTunes/iTunes Media/TV Shows/12 Monkeys/Season 01/08 明天.m4v
- ./iTunes/iTunes Media/TV Shows/12 Monkeys/Season 01/09 昨天.m4v
被link编辑为:
- ./TVShows/12.Monkeys/Season.01/12.Monkeys.s01e00.Tomorrow.m4v
- ./TVShows/12.Monkeys/Season.01/12.Monkeys.s01e00.Yesterday.m4v
谁能告诉我为什么会这样,我该如何解决?
printf %02d 08 09
失败
bash: printf: 08: invalid octal number
bash: printf: 09: invalid octal number
那是因为以 0 开头的数字被解释为八进制数,而八进制数只能包含字符 0-7。删除前导零。在bash中,您可以使用
x=008
printf %02d $((10#$x))
将数字解释为十进制。
您也可以使用$(printf %s "$x" | sed 's/^00*//')
我一直在尝试使用以下脚本来 link 我 iTunes 中的所有电视节目,例如:
- ./iTunes/iTunes Media/TV Shows/Rookie Blue/Season 5/01 Blink.m4v
- ./iTunes/iTunes Media/TV Shows/Haven/Season 3/11 最后的告别.m4v
- ./iTunes/iTunes Media/TV Shows/Doctor 谁 (2005)/第 5/10 季 Vincent and the Doctor.m4v
到新目录,格式为./TVShows/Show.Name/Season.nn/Show.Name.SnnEnn.Episode.Name.m4v 例如:
- ./TVShows/Rookie.Blue/Season.05/Rookie.Blue.S05E01.Blink.m4v
- ./TVShows/Haven/Season.03/Haven.S03E11.Last.Goodbyes.m4v
- ./TVShows/TV Shows/Doctor.Who.2005/Season.05/Doctor.Who.2005.S05E10.Vincent.and.the.Doctor.m4v
#! /bin/bash
srcroot="./iTunes/iTunes Media/TV Shows"
destroot="./TVShows"
for f in "$srcroot"/*/*/*.{m4v,mp4,avi}
do
[[ -f $f ]] || continue
IFS=/ read -a vid <<< "${f// /.}"
vid=( "${vid[@]: -3}" )
show=${vid[0]}
season=${vid[1]}
episode=${vid[2]}
[[ $show =~ \([0-9]{4}\)$ ]] && show=${show: 0:${#show}-6}${show: ${#show}-5:4}
sn=${season#*.}
IFS=. read ep eptitle <<< "${episode%.*}"
ext=${episode##*.}
printf -v hldir -- '%s/%s/%s' "$destroot" "$show" "$season"
printf -v hlname -- '%s.s%02de%02d.%s.%s' "$show" "$sn" "$ep" "$eptitle" "$ext"
echo "from: '$f'"
echo "to: '$hldir/$hlname'"
mkdir -p "$hldir"
ln "$f" "$hldir/$hlname"
echo
done
除了遇到第 08 或 09 集外,这个脚本似乎大部分都符合我的要求 例如:
- ./iTunes/iTunes Media/TV Shows/12 Monkeys/Season 01/08 明天.m4v
- ./iTunes/iTunes Media/TV Shows/12 Monkeys/Season 01/09 昨天.m4v
被link编辑为:
- ./TVShows/12.Monkeys/Season.01/12.Monkeys.s01e00.Tomorrow.m4v
- ./TVShows/12.Monkeys/Season.01/12.Monkeys.s01e00.Yesterday.m4v
谁能告诉我为什么会这样,我该如何解决?
printf %02d 08 09
失败
bash: printf: 08: invalid octal number
bash: printf: 09: invalid octal number
那是因为以 0 开头的数字被解释为八进制数,而八进制数只能包含字符 0-7。删除前导零。在bash中,您可以使用
x=008
printf %02d $((10#$x))
将数字解释为十进制。
您也可以使用$(printf %s "$x" | sed 's/^00*//')