Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading