[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: getline in /proc
From: |
Simon Vaillancourt |
Subject: |
Re: getline in /proc |
Date: |
Sat, 17 Sep 2005 11:19:35 -0600 (MDT) |
Yikes, I got that from a sample in a tutorial, I should have RTFM, very sorry
to have bothered you with that.
Simon
__________________________________________________
>From Stepan Kasal <address@hidden>
Sent Sat 9/17/2005 10:23 AM
To Simon Vaillancourt <address@hidden>
Cc address@hidden
Subject Re: getline in /proc
__________________________________________________
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