读取while循环输出的其余部分

时间:2015-03-30 10:16:26

标签: bash loops

我想从MySQL输出的输出中运行while循环,但我的输出被切断了。

我从MySQL获得的示例输出是:

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

我的循环看起来像这样:

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

$ {b}的结果在nfs://192.168.1.100/full/A之后被切断。 我如何输出整个句子?

2 个答案:

答案 0 :(得分:2)

您的第二个文件名包含空格,因此这是字段被截断的位置。 由于它是输出的最后一个字段,因此您可以跳过字段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

读取的最后一个字段将包含所有剩余字段。

答案 1 :(得分:1)

问题是您使用以下方法将每行读入3个变量:

read a b c

因为你的输入行也包含一个空格,例如

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

使用默认IFS,它将3个变量设置为:

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

由于cread中的最后一个参数,因此它会在c中读取其余部分。

要修复脚本,您可以这样做:

read a b
相关问题