euglug.org

[Eug-lug] Reading data to multiple variables

작성자

카테고리:



Neil ParkerWed Jul 28 22:14:06 PDT 2004

Garl Grigsby wrote,
>The way you had it the read would stop just after the Protocol line. It 
>would never process the Transport, GUID, or the Attached lines. Again, I 
>have no idea why this is happening. I can't find anything in there that 
>would cause it to fail but it did. Any way, this works, and works well.

That's probably due to the fact that the files in /proc aren't really files--
they're magical names that the kernel recognizes as requests to print out
data.  Some of them are implemented rather crudely--they dump their output
in big hunks, and expect the reader to read them in equally big hunks, and
if you try to read in smaller hunks (e.g. one line at a time), you miss
data.

Not all of the /proc files have this problem, but some do.  This is why
some Linux HOWTOs recommend commands like "cat /proc/whatever|more"
instead of "more /proc/whatever".

On my machine (kernel 2.2.23), some of the IDE info files have this
problem.  For example,

     $ cd /proc/ide/ide0/hda
     $ cat geometry
     physical     16383/16/63
     logical      1247/255/63
     $ while read line;do echo $line;done<geometry
     physical 16383/16/63
     $ cat geometry|while read line;do echo $line;done
     physical 16383/16/63
     logical 1247/255/63
     $

The pipe works because "cat" reads its input in large-enough chunks, and feeds
it to a pipe, which you can read in any size chunks you want without
missing data.

          - Neil Parker