如何在Bash

时间:2015-10-06 22:17:27

标签: linux bash shell repeat

如何在同一代码中多次运行?例如12次?

 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

所有代码必须重复N次

类似

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

但它不适用于多行命令。

任何问题?

2 个答案:

答案 0 :(得分:0)

您可以使用seq

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

它不是内置的,但它是一种非常常用的功能。

答案 1 :(得分: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