Stephen
Stephen

Reputation: 149

Read rest of while loop output

I want to run a while loop from output I get from MySQL, but my output is being cut off.

Example output I get from MySQL is:

123 nfs://192.168.1.100/full/Some.file.1.txt
124 nfs://192.168.1.100/full/A second file 2.txt

My loop looks like so:

mysql -uuser -ppass queue -ss -e 'select id,path from queue where status = 0' | while read a b c 
do
    echo $a
    echo $b
done

The result for $b cuts off after nfs://192.168.1.100/full/A. How can I have it output the whole sentence?

Upvotes: 0

Views: 603

Answers (2)

anubhava
anubhava

Reputation: 785186

Problem is that you are reading each line into 3 variables using:

read a b c

And since your input line also contains a whitespace e.g.

124 nfs://192.168.1.100/full/A second file 2.txt

with the default IFS it is setting 3 variables as:

a=124
b=nfs://192.168.1.100/full/A
c=second file 2.txt

Since c is the last parameter in read it is reading rest of the line in c.

To fix your script you can just do:

read a b

Upvotes: 1

Walter A
Walter A

Reputation: 20002

Your second filename contains spaces, so that is where the field is cut off. Since it is the last field of the output, you can just skip field c:

mysql -uuser -ppass queue -ss -e 'select id,path from queue where status = 0' | while read a b
do
   echo $a
   echo $b
done

The last field in read will have all remaining fields.

Upvotes: 2

Related Questions