2015-10-06 31 views
0

我怎麼能多次運行相同的代碼?例如12次?如何運行多時間相同的代碼中的Bash

sed -n 1,1p 00-02.txt | sed -e 's/^/<video length="-1" src="mp4:/' \ 
          -e 's/$/" start="0"><\/video>/' >>playlist.txt 

    echo -e "$(sed -e '1,1d' 00-02.txt)\n" > 00-02.txt 

    cat 00-02.txt | sed '/^$/d' >> 00-02a.txt 

    rm 00-02.txt 

    mv 00-02a.txt 00-02.txt 

    sed -n 1,1p spot.txt | sed -e 's/^/<video length="-1" src="mp4:/' \ 
          -e 's/$/" start="0"><\/video>/' >>playlist.txt 

    echo -e "$(sed -e '1,1d' spot.txt)\n" > spot.txt 

    cat spot.txt | sed '/^$/d' >> spota.txt 

    rm spot.txt 

mv spota.txt spot.txt 

所有代碼belove必須複製N次

for n in {1..12}; **ALL THE COMMANDS BELOVE**; done 

但它不爲命令的多行工作。

任何問題?

+0

我應該在工作for循環,你能解釋一下你的意思由不爲多行工作?你有錯誤還是你得到錯誤的結果? – Ajay

+0

謝謝@Ajay解答下面的答案。 –

回答

0

使用while循環和計數器:

#!/bin/bash 

iterations=12 
count=0 

while [ "$count" -lt "$iterations" ] 
do 
    sed -n 1,1p 00-02.txt | sed -e 's/^/<video length="-1" src="mp4:/' \ 
         -e 's/$/" start="0"><\/video>/' >>playlist.txt 

    echo -e "$(sed -e '1,1d' 00-02.txt)\n" > 00-02.txt 

    cat 00-02.txt | sed '/^$/d' >> 00-02a.txt 

    rm 00-02.txt 

    mv 00-02a.txt 00-02.txt 

    sed -n 1,1p spot.txt | sed -e 's/^/<video length="-1" src="mp4:/' \ 
         -e 's/$/" start="0"><\/video>/' >>playlist.txt 

    echo -e "$(sed -e '1,1d' spot.txt)\n" > spot.txt 

    cat spot.txt | sed '/^$/d' >> spota.txt 

    rm spot.txt 

    mv spota.txt spot.txt 
    count=$((count + 1)) 
done 
+0

它的工作原理!解決了! –

0

你可以使用seq

for i in $(seq 1 12) ; do echo $i ; done 

這不是一個內置的,但它是一個很常見的實用工具。

+0

我應該放置所有的代碼行嗎? –