如何 运行 在 Bash 中多次使用相同的代码

How to Run Multiple Time Same Code in Bash

我如何 运行 多次使用相同的代码?比如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

所有代码belove必须复制N次

类似于

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

但它不适用于多行命令。

有什么问题吗?

您可以使用 seq:

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

它不是内置的,但它是一个非常常用的实用程序。

使用 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