Search code examples
linuxbashrhel

While loop in bash using variable from txt file


I am new to bash and writing a script to read variables that is stored on each line of a text file (there are thousands of these variables). So I tried to write a script that would read the lines and automatically output the solution to the screen and save into another text file.

./reader.sh > solution.text

The problem I encounter is currently I have only 1 variable store in the Sheetone.txt for testing purpose which should take about 2 seconds to output everything but it is stuck in the while loop as well as is not outputting the solution.

#!/bin/bash
file=Sheetone.txt
while IFS= read -r line
do
        echo sh /usr/local/test/bin/test -ID $line -I 
done

Solution

  • As indicated in the comments, you need to provide "something" to your while loop. The while construct is written in a way that will execute with a condition; if a file is given, it will proceed until the read exhausts.

    #!/bin/bash
    file=Sheetone.txt
    while IFS= read -r line
    do
        echo sh /usr/local/test/bin/test -ID $line -I 
    done < "$file"
    # -----^^^^^^^ a file!
    

    Otherwise, it was like cycling without wheels...