0

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?

2
  • 1
    What do you want to be in variable c? Commented Mar 30, 2015 at 10:24
  • I don't want or expect anything, its just a force of habit. I tried it without c and now it works! Can you add it as an answer (not a comment) so I can mark it as answered? Commented Mar 30, 2015 at 10:30

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.