2011-03-03 160 views
1

我不知道用我的bash腳本來選擇命令的數組。在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. 

回答

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")' 
+0

感謝簡化了劇本,但對於A = $(-i了mkvmerge輸入.mkv | WC-1),你缺少的grep -i附件,整條生產線應該是A = $(了mkvmerge - 我輸入.mkv | grep -i附件| wc -l) – dewaforex 2011-03-03 07:21:23

+0

@dewaforex:哎呀,對不起。我過分熱心了。 – 2011-03-03 07:23:39

+0

不,問題。現在它更簡單了,但仍然無法弄清楚當把雙重qoute放在Ignacio的回答指向的「$ {attachment [*]}」時的最後一個錯誤 – dewaforex 2011-03-03 07:36:00