How to run scripts one at a time inside bash while loop

I have the following Bash script:

BITRATE=
QUALITY=

while read -r BITRATE QUALITY; do
        source ./quality-test.sh -b "$BITRATE" -q "$QUALITY"
done < parameters.txt

I expected this while loop to run quality-test.sh for every line in parameters.txt; however, even though parameters.txt contains more than one line, the while loop runs only once (I’ve checked it by printing the values of BITRATE and QUALITY before and after the script). Why is this so? I have tried multiple ways, with or without source, having source in its own (...) environment etc. but nothing works. quality-test.sh works perfectly fine by itself, if I run it from terminal ./quality-test.sh -b ... -q ... produces the expected results. How can I solve this? I cannot run quality-test.sh in the background, as multiple instances of the script would interfere with each other.

>Solution :

The most likely reason is that quality-test.sh is reading from standard input, so it’s consuming the rest of the file. Redirect its input so it doesn’t do this.

while read -r BITRATE QUALITY; do
    source ./quality-test.sh -b "$BITRATE" -q "$QUALITY" < /dev/null
done < parameters.txt

Leave a Reply