Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Loading STDOUT into a variable

1 view
Skip to first unread message

Stephan Bour

unread,
Mar 31, 2003, 4:25:11 PM3/31/03
to
I need to parse the output of the ifconfig shell command
(system("ifconfig");) for the current IP address. I know I can redirect the
STDOUT to a temp file and work on that to extract the info I need but is
there any way to assign the content of STDOUT to a variable without writing
it to a file? Can I do this: while (<STDOUT>) ...?
Thank you,
Stephan.

Steve Grazzini

unread,
Mar 31, 2003, 4:45:33 PM3/31/03
to
Stephan Bour <sb...@niaid.nih.gov> wrote:
> I need to parse the output of the ifconfig shell command
> (system("ifconfig");) for the current IP address. I know
> I can redirect the STDOUT to a temp file and work on that
> to extract the info I need but is there any way to assign
> the content of STDOUT to a variable without writing it to
> a file?

FAQ:

$ perldoc -q "with system"
Found in /usr/local/lib/perl5/5.8.0/pod/perlfaq8.pod
Why can't I get the output of a command with system()?

--
Steve

Sisyphus

unread,
Mar 31, 2003, 4:43:22 PM3/31/03
to

"Stephan Bour" <sb...@niaid.nih.gov> wrote in message
news:BAAE1C67.6BA4%sb...@niaid.nih.gov...

Just do:
my $output = `ifconfig`;
Then parse $output.

Cheers,
Rob


Jürgen Exner

unread,
Mar 31, 2003, 8:11:20 PM3/31/03
to
Stephan Bour wrote:
> I need to parse the output of the ifconfig shell command
> (system("ifconfig");) for the current IP address.

Why don't you just read the man page for the functions you are using?
perldoc -f system
and the answer to your question is right there in the third paragraph

jue


Tad McClellan

unread,
Mar 31, 2003, 10:02:28 PM3/31/03
to
Stephan Bour <sb...@niaid.nih.gov> wrote:

> system("ifconfig");

> there any way to assign the content of STDOUT to a variable


perldoc -f system


*plonk*


--
Tad McClellan SGML consulting
ta...@augustmail.com Perl programming
Fort Worth, Texas

Stephan Bour

unread,
Apr 1, 2003, 8:56:09 AM4/1/03
to
in article slrnb8i0a4...@magna.augustmail.com, Tad McClellan at
ta...@augustmail.com wrote on 3/31/03 22:02:

> Stephan Bour <sb...@niaid.nih.gov> wrote:
>
>> system("ifconfig");
>
>> there any way to assign the content of STDOUT to a variable
>
>
> perldoc -f system
>
>
> *plonk*
>

Thanks to those who actually provided help. I realize that most of the
newbie questions I (and others) ask can be solved by reading a FAQ or help
file somewhere. What you pros have to understand is that beginners don't
always know where and how to look for those simple answers. I never post a
question before doing my very best to answer it myself with the resources I
have. Please forgive me if that sometimes is not good enough.
Cheers,

Stephan.

Tad McClellan

unread,
Apr 1, 2003, 4:18:55 PM4/1/03
to
Stephan Bour <sb...@niaid.nih.gov> wrote:
> in article slrnb8i0a4...@magna.augustmail.com, Tad McClellan at
> ta...@augustmail.com wrote on 3/31/03 22:02:
>
>> Stephan Bour <sb...@niaid.nih.gov> wrote:
>>
>>> system("ifconfig");
>>
>>> there any way to assign the content of STDOUT to a variable
>>
>> perldoc -f system
>>
>> *plonk*
>>
> Thanks to those who actually provided help.


You're welcome.

( the help I offered was: you should read the docs for the functions
that you use, else you will not know what to expect when you
call them.
)


> I realize that most of the
> newbie questions I (and others) ask can be solved by reading a FAQ or help
> file somewhere. What you pros have to understand is that beginners don't
> always know where and how to look for those simple answers.


I already do understand that.

I don't killfile for asking a FAQ (and that isn't what you did).

I killfile people that sign contracts without reading them,
which _is_ what you did.

Now I know that in the real world folks don't read the docs for
the functions they invoke, but at the *very first* sign of
trouble they should know to go fix that.

Those that don't will be a time-sink that draws time away from
more deserving posters.


> I never post a
> question before doing my very best to answer it myself with the resources I
> have. Please forgive me if that sometimes is not good enough.


Are you saying that you did not know how to look up Perl functions?

Stephan Bour

unread,
Apr 2, 2003, 11:02:18 AM4/2/03
to
in article slrnb8k0hv...@magna.augustmail.com, Tad McClellan at
ta...@augustmail.com wrote on 4/1/03 16:18:

I do consider telling me about perldoc help, so thanks. I didn't know about
that resource and I'm glad it was pointed out to me. In my struggle to learn
my first programming language I mostly rely on books but sometimes even the
simplest explanations in those beginner books are unclear to me because I
have no programming experience. I am a scientist making an exciting but
sometimes painful transition to bioinformatics and I'm pretty isolated. The
fact is, everyone struggles with the post or don't post question: should I
ask a question to the list if I know I could possibly find the answer myself
with two hours of intense guess work while it would take a knowledgeable
person 5 minutes to point me in the right direction? You tell me.
Cheers,
Stephan.

Tad McClellan

unread,
Apr 3, 2003, 12:27:47 AM4/3/03
to
Stephan Bour <sb...@niaid.nih.gov> wrote:
> in article slrnb8k0hv...@magna.augustmail.com, Tad McClellan at
> ta...@augustmail.com wrote on 4/1/03 16:18:
>> Stephan Bour <sb...@niaid.nih.gov> wrote:
>>> in article slrnb8i0a4...@magna.augustmail.com, Tad McClellan at
>>> ta...@augustmail.com wrote on 3/31/03 22:02:
>>>
>>>> Stephan Bour <sb...@niaid.nih.gov> wrote:
>>>>
>>>>> system("ifconfig");
>>>>
>>>>> there any way to assign the content of STDOUT to a variable
>>>>
>>>> perldoc -f system
>>>>
>>>> *plonk*

>> Are you saying that you did not know how to look up Perl functions?
>>
>
> I do consider telling me about perldoc help, so thanks.


You're welcome. If you didn't know that, then there are likely to
be other helpful tidbits in the Posting Guidelines:

http://mail.augustmail.com/~tadmc/clpmisc.shtml


> I didn't know about
> that resource and I'm glad it was pointed out to me. In my struggle to learn
> my first programming language I mostly rely on books


Books are a third level resource.

First is the documentation that came with the software you are using.

Second is a Google groups search.


> but sometimes even the
> simplest explanations in those beginner books are unclear to me because I
> have no programming experience.


The usual Perl tutorial book recommendation for non-programmers is:

"Elements of Programming with Perl" by Andrew Johnson

Is that one of the books you have?


> fact is, everyone struggles with the post or don't post question: should I
> ask a question to the list if I know I could possibly find the answer myself
> with two hours


You are not expected to spend 2 hours before posting, more like 10 minutes.


> of intense guess work


I think the problem we're having here is that not reading the docs
for a function that you use is so fundamental that a programmer
cannot envision someone that does not know that, so it "looks like"
the Bad Kind Of Lazy ie. you want us to look it up for you.

We should perhaps keep in mind that not being able to envision
something does not mean that the something does not exist. :-)

(how's that for a triple negative? Makes your head hurt, doesn't it?)


> while it would take a knowledgeable
> person 5 minutes to point me in the right direction?


5 minutes from the answerer
200 minutes for every reader of the group to scan the question
(6 seconds: 0.1 * 2000 people)

205 minutes to get your answer.

The Questioner and the Answerer are not the only participants.

Stephan Bour

unread,
Apr 3, 2003, 8:55:50 AM4/3/03
to
in article slrnb8nhij...@magna.augustmail.com, Tad McClellan at
ta...@augustmail.com wrote on 4/3/03 00:27:

Snip... I have: Learning Perl (O'Reilly), Perl A Beginner's Guide (Osborne),
Perl 5 Pocket Reference (O'Reilly), Programming Perl (O'Reilly) and Perl
Cookbook (O'Reilly). I guess I need one more...
Thanks.

0 new messages