用于bash脚本中命令选项的数组输出

时间:2011-03-03 05:53:07

标签: bash encoding attachment mkv

我用我的bash脚本和数组作为命令选项搞砸了。

我制作bash脚本以从mkv文件中提取附件,并在视频/音频编码完成后再次合并mkv文件的附件。

这是用于提取附件

#find the total of attachment
A=$(mkvmerge -i input.mkv | grep -i attachment 
                          | awk '{printf $3 "\n"}' 
                          | sed 's;\:;;' 
                          | awk 'END { print NR }')

#extract it
for (( i=1; i<=$A; i++ ))
do
font[${i}]="$(mkvmerge -i input.mkv | grep -i attachment 
                                    | awk '{for (i=11; i <= NF; i++) printf($i"%c" , (i==NF)?ORS:OFS) }' 
                                    | sed "s/'//g" 
                                    | awk "NR==$i")"

mkvextract attachments input.mkv $i:"${font[${i}]}"
done

现在再次合并附件

for (( i=1; i<=$A; i++ ))
do
#seach for space between file name and and '\' before 
#the space because some attachment has space in filename
font1[${i}]=$(echo ${font[${i}]} | sed 's/ /\\ /g')
#make option for add attachment
attachment[${i}]=$"--attach-file ${font1[${i}]}"
done

mkvmerge -o output.mkv -d 1 -S test.mp4 sub.ass ${attachment[*]}

问题,仍然无法用于带空格的文件名。 当我尝试回复${attachment[*]}时,它似乎没问题

--attach-file Beach.ttf --attach-file Candara.ttf 
                        --attach-file CASUCM.TTF 
                        --attach-file Complete\ in\ Him.ttf 
                        --attach-file CURLZ_.TTF 
                        --attach-file Frostys\ Winterland.TTF 
                        --attach-file stilltim.ttf

但输出仍然识别文件名,空格只有第一个单词。

mkvmerge v3.0.0 ('Hang up your Hang-Ups') built on Dec  6 2010 19:19:04
Automatic MIME type recognition for 'Beach.ttf': application/x-truetype-font
Automatic MIME type recognition for 'Candara.ttf': application/x-truetype-font
Automatic MIME type recognition for 'CASUCM.TTF': application/x-truetype-font
Error: The file 'Complete\' cannot be attached because it does not exist or cannot be read.

1 个答案:

答案 0 :(得分:0)

Ignacio的回答会指出你正确的方向,但我想对你脚本中的一些事情发表评论。

这条线有很多无意义的回转:

A=$(mkvmerge -i input.mkv | grep -i attachment | awk '{printf $3 "\n"}' | sed 's;\:;;' | awk 'END { print NR }')

这里简化了:

A=$(mkvmerge -i input.mkv | grep -i attachment | wc -l)

没有必要为数组索引使用美元符号和大括号:

attachment[i]="--attach-file ${font1[i]}"

此外,$""用于创建翻译字符串(i18n和l10n)。你可能不需要那里。但是,这是您应该更改的行之一,因为它是您问题的一部分。您将发现使用sed命令在此之前的行是不必要的。

修改

如果你真的需要使用数组:

for (( i=1; i<=A; i++ ))
do
    #make option for add attachment
    attachment+=("--attach-file" "${font1[i]}")
done

mkvmerge -o output.mkv -d 1 -S test.mp4 sub.ass "${attachment[@]}"

生成的attachment数组的元素数量是font1数组的两倍。

$ declare -p font1 attachment    # show the contents
declare -a font1='([0]="Beach.ttf" [1]="Candara.ttf" [2]="CASUCM.TTF" [3]="Complete" [4]="in" [5]="Him.ttf" [6]="CURLZ_.TTF" [7]="Frostys" [8]="Winterland.TTF" [9]="stilltim.ttf")'
declare -a attachment='([0]="--attach-file" [1]="Beach.ttf" [2]="--attach-file" [3]="Candara.ttf" [4]="--attach-file" [5]="CASUCM.TTF" [6]="--attach-file" [7]="Complete in Him.ttf" [8]="--attach-file" [9]="CURLZ_.TTF" [10]="--attach-file" [11]="Frostys Winterland.TTF" [12]="--attach-file" [13]="stilltim.ttf")'