Skip to content
Advertisement

How to Run Multiple Time Same Code in Bash

How can i run multiple times this same code? For example 12 times?

 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

All the code belove must be replicated N times

something like

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

But it does not work for multiple lines of command.

Any issue?

Advertisement

Answer

Using a while loop and a counter:

#!/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
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement