bug-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: getline in /proc


From: Stepan Kasal
Subject: Re: getline in /proc
Date: Sat, 17 Sep 2005 18:23:28 +0200
User-agent: Mutt/1.4.1i

Hello,

On Fri, Sep 16, 2005 at 03:11:37PM -0400, Simon Vaillancourt wrote:
> echo test | gawk 'BEGIN { while (getline < 
> "/proc/anypid/unexistingfile") {print "line="$0}}'

the manpage says:
"The getline command returns 0 on end of file and -1 on an error."

Thus your code contains a bug.  I think this bug occures quite often.

Corrected version:

gawk 'BEGIN { while ( (getline < "/proc/anypid/unexistingfile") > 0 )
        {print "line="$0}}'

And, since it's a good habit to close the files when you are done,
it's customary to use a variable for the file name:

gawk 'BEGIN { a_file = "/proc/anypid/unexistingfile"
        while ( (getline <a_file) > 0 ) {
                print "line="$0
        }
}'

Have a nice day,
        Stepan Kasal




reply via email to

[Prev in Thread] Current Thread [Next in Thread]