SOS...on system command

Discussion in 'Perl Misc' started by cyrusgreats@gmail.com, Jul 17, 2008.

  1. Guest

    I'm trying to store out put into the array the search for string in
    this case truncating, but when I run the following command it prints
    all the output before going to the foreach loop, it was my
    understanding that the out put of the of the command will be saved
    into @output then it can be searched, can some tell me where I go
    wrong on this..thanks in advance...


    my $sys = "dude";
    my @output= `snmpwalk -c public -v 1 $sys .1 |grep \"truncating\"`;

    foreach my $el (@output) {
    next if ($el !=m~ ^\truncating\);
    print " .................$el\n";
    }
    , Jul 17, 2008
    #1
    1. Advertising

  2. J. Gleixner Guest

    wrote:

    Next time please post a helpful subject.

    > I'm trying to store out put into the array the search for string in
    > this case truncating, but when I run the following command it prints
    > all the output before going to the foreach loop, it was my
    > understanding that the out put of the of the command will be saved
    > into @output then it can be searched, can some tell me where I go
    > wrong on this..thanks in advance...
    >
    >
    > my $sys = "dude";
    > my @output= `snmpwalk -c public -v 1 $sys .1 |grep \"truncating\"`;
    >
    > foreach my $el (@output) {
    > next if ($el !=m~ ^\truncating\);

    ^^^^^^.. first, what do you expect that to do?
    > print " .................$el\n";
    > }
    J. Gleixner, Jul 17, 2008
    #2
    1. Advertising

  3. Guest

    On Jul 17, 1:45 pm, "J. Gleixner" <>
    wrote:
    > wrote:
    >
    > Next time please post a helpful subject.
    >
    > > I'm trying to store out put into the array the search for string in
    > > this case truncating, but when I run the following command it prints
    > > all the output before going to the foreach loop, it was my
    > > understanding that the out put of the of the command will be saved
    > > into @output then it can be searched, can some tell me where I go
    > > wrong on this..thanks in advance...

    >
    > > my $sys = "dude";
    > > my @output= `snmpwalk -c public -v 1 $sys .1 |grep \"truncating\"`;

    >
    > > foreach my $el (@output) {
    > > next if ($el !=m~ ^\truncating\);

    >
    > ^^^^^^.. first, what do you expect that to do?
    >
    > > print " .................$el\n";
    > > }


    My questions are:
    1. why it print everything right after this "my @output= `snmpwalk -c
    public -v 1 $sys .1 |grep \"truncating\"`; " instead of storing it
    into @output array
    2. Why the @output is empty?

    Sorry for bad subject, next time I'll try do better ;-)
    , Jul 17, 2008
    #3
  4. writes:

    > On Jul 17, 1:45 pm, "J. Gleixner" <>
    > wrote:
    >> wrote:
    >>
    >> Next time please post a helpful subject.
    >>
    >> > I'm trying to store out put into the array the search for string in
    >> > this case truncating, but when I run the following command it prints
    >> > all the output before going to the foreach loop, it was my
    >> > understanding that the out put of the of the command will be saved
    >> > into @output then it can be searched, can some tell me where I go
    >> > wrong on this..thanks in advance...

    >>
    >> > my $sys = "dude";
    >> > my @output= `snmpwalk -c public -v 1 $sys .1 |grep \"truncating\"`;

    >>
    >> > foreach my $el (@output) {
    >> > next if ($el !=m~ ^\truncating\);

    >>
    >> ^^^^^^.. first, what do you expect that to do?
    >>
    >> > print " .................$el\n";
    >> > }

    >
    > My questions are:
    > 1. why it print everything right after this "my @output= `snmpwalk -c
    > public -v 1 $sys .1 |grep \"truncating\"`; " instead of storing it
    > into @output array


    Because your check is incorrect. I'm VERY surprised that code even
    compiles. Please enable warnings and strict. Anyway, you probably want something like

    next if ($el =~ /^truncating/);

    > 2. Why the @output is empty?


    I'm sure it isn't.


    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
    Joost Diepenmaat, Jul 17, 2008
    #4
  5. Guest

    wrote:
    > I'm trying to store out put into the array the search for string in
    > this case truncating, but when I run the following command it prints
    > all the output before going to the foreach loop, it was my
    > understanding that the out put of the of the command will be saved
    > into @output then it can be searched, can some tell me where I go
    > wrong on this..thanks in advance...
    >
    > my $sys = "dude";
    > my @output= `snmpwalk -c public -v 1 $sys .1 |grep \"truncating\"`;


    I can't reproduce your problem. Things for me work as expected, once I
    replace snmpwalk with a command I trust to run on my computer which still
    generates output containing "truncating" on some lines.

    Are you sure your real code uses backticks, as shown above, and not system,
    as your subject line would seem to suggest?


    > foreach my $el (@output) {
    > next if ($el !=m~ ^\truncating\);


    What the heck is that supposed to be? It doesn't compile, whatever it
    is.


    > print " .................$el\n";
    > }


    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
    , Jul 17, 2008
    #5
  6. wrote:
    > On Jul 17, 1:45 pm, "J. Gleixner" <>
    > wrote:
    > > wrote:
    > >
    > > Next time please post a helpful subject.
    > >
    > > > I'm trying to store out put into the array the search for string in
    > > > this case truncating, but when I run the following command it prints
    > > > all the output before going to the foreach loop, it was my
    > > > understanding that the out put of the of the command will be saved
    > > > into @output then it can be searched, can some tell me where I go
    > > > wrong on this..thanks in advance...

    > >
    > > > my $sys = "dude";
    > > > my @output= `snmpwalk -c public -v 1 $sys .1 |grep \"truncating\"`;

    > >
    > > > foreach my $el (@output) {
    > > > next if ($el !=m~ ^\truncating\);

    > >
    > > ^^^^^^.. first, what do you expect that to do?
    > >
    > > > print " .................$el\n";
    > > > }


    > My questions are:
    > 1. why it print everything right after this "my @output= `snmpwalk -c
    > public -v 1 $sys .1 |grep \"truncating\"`; " instead of storing it
    > into @output array
    > 2. Why the @output is empty?


    with the backticks you create a new process that starts a shell
    to run the command. the output of this command is printed to
    the stdout of that shell and is not passed back to your script.
    That's what you're seeing when you run your script, the output
    of the command goes to the terminal and not your script. All
    your script receives is the return value of the last command
    that got executed. If you want the output of a command then
    don't use backticks but use open with the output of the com-
    mand redirected to a file handle that your script can read
    from:

    open my $f, '|-', 'my_command' or die "Can't execute my_command\n";

    Then read in what the command outputs just like it would be a
    normal file:

    my @output = <$f>;

    and finally close this "file":

    close $f;

    Now '@output' contains all the lines output by the command.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Jul 17, 2008
    #6
  7. Jens Thoms Toerring <> wrote:
    > wrote:
    > > On Jul 17, 1:45 pm, "J. Gleixner" <>
    > > wrote:
    > > > wrote:
    > > >
    > > > Next time please post a helpful subject.
    > > >
    > > > > I'm trying to store out put into the array the search for string in
    > > > > this case truncating, but when I run the following command it prints
    > > > > all the output before going to the foreach loop, it was my
    > > > > understanding that the out put of the of the command will be saved
    > > > > into @output then it can be searched, can some tell me where I go
    > > > > wrong on this..thanks in advance...
    > > >
    > > > > my $sys = "dude";
    > > > > my @output= `snmpwalk -c public -v 1 $sys .1 |grep \"truncating\"`;
    > > >
    > > > > foreach my $el (@output) {
    > > > > next if ($el !=m~ ^\truncating\);
    > > >
    > > > ^^^^^^.. first, what do you expect that to do?
    > > >
    > > > > print " .................$el\n";
    > > > > }


    > > My questions are:
    > > 1. why it print everything right after this "my @output= `snmpwalk -c
    > > public -v 1 $sys .1 |grep \"truncating\"`; " instead of storing it
    > > into @output array
    > > 2. Why the @output is empty?


    > with the backticks you create a new process that starts a shell
    > to run the command. the output of this command is printed to
    > the stdout of that shell and is not passed back to your script.


    Sorry, forget about that. Got things mixed up with the system()
    function you were refering to in the subject line. Things are
    different with backticks.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Jul 17, 2008
    #7
  8. Ted Zlatanov Guest

    On Thu, 17 Jul 2008 15:45:38 -0500 "J. Gleixner" <> wrote:

    JG> wrote:
    >> next if ($el !=m~ ^\truncating\);

    JG> ^^^^^^.. first, what do you expect that to do?

    Obviously, it inverts a negative hopeful match in rleP :)

    I do wish people would post more rleP code.

    Ted
    Ted Zlatanov, Jul 18, 2008
    #8
  9. wrote:
    > On Jul 17, 1:45 pm, "J. Gleixner" <>
    > wrote:
    >> wrote:

    *SKIP*
    >> > my @output= `snmpwalk -c public -v 1 $sys .1 |grep \"truncating\"`;

    *SKIP*
    > 1. why it print everything right after this "my @output= `snmpwalk -c
    > public -v 1 $sys .1 |grep \"truncating\"`; " instead of storing it
    > into @output array
    > 2. Why the @output is empty?


    In case you are B<absolutely> sure that I<@output> is empty (prove you
    are), then (if my crystal globe doesn't joke this time) I would
    investigate if B<snmpwalk> uses C<STDOUT> for output (instead of some
    other handle).

    --
    Torvalds' goal for Linux is very simple: World Domination
    Eric Pozharski, Jul 18, 2008
    #9
  10. Eric Pozharski wrote:
    > In case you are B<absolutely> sure that I<@output> is empty (prove you
    > are), then (if my crystal globe doesn't joke this time) I would
    > investigate if B<snmpwalk> uses C<STDOUT> for output (instead of some
    > other handle).

    That'd be my guess too. Depending on your shell you might try:

    my @output= `snmpwalk -c public -v 1 $sys .1 2>&1 |grep \"truncating\"`;

    You could always do the following in the shell:

    $ snmpwalk -c public -v 1 </sys/> .1 1> stdout 2> stderr

    Then check what goes to stdout and what goes to stderr.
    --
    Andrew DeFaria <http://defaria.com>
    Puritanism: The haunting fear that someone, somewhere may be happy.
    Andrew DeFaria, Jul 20, 2008
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. walala
    Replies:
    4
    Views:
    1,177
    Technology Consultant
    Sep 9, 2003
  2. walala
    Replies:
    1
    Views:
    635
    steve
    Sep 12, 2003
  3. kpk
    Replies:
    37
    Views:
    16,657
    Bob Stephens
    Jan 6, 2004
  4. Kenny

    SOS - SourceSafe

    Kenny, Dec 5, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    583
    Chris Jackson
    Dec 5, 2003
  5. Giovane Calabrese

    Multilanguage System (TAKE LABEL NAME) SOS

    Giovane Calabrese, Dec 15, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    417
    Giovane Calabrese
    Dec 15, 2003
Loading...

Share This Page