在bash脚本中输入命令选项的数组输出
问题描述:
我不知道用我的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.
答
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")'
感谢简化了剧本,但对于A = $(-i了mkvmerge输入.mkv | WC-1),你缺少的grep -i附件,整条生产线应该是A = $(了mkvmerge - 我输入.mkv | grep -i附件| wc -l) – dewaforex 2011-03-03 07:21:23
@dewaforex:哎呀,对不起。我过分热心了。 – 2011-03-03 07:23:39
不,问题。现在它更简单了,但仍然无法弄清楚当把双重qoute放在Ignacio的回答指向的“$ {attachment [*]}”时的最后一个错误 – dewaforex 2011-03-03 07:36:00