Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Grabbing tokens

by kidwahoo94 (Novice)
on May 13, 2003 at 13:56 UTC ( #257732=perlquestion: print w/replies, xml ) Need Help??

kidwahoo94 has asked for the wisdom of the Perl Monks concerning the following question:

I have some sample like this -bam-
system ("net use J\: \\\\$hostname\\c\$");
It will print 'Command finished successfully' on the screen. How could I grab the third 'token' ('successfully') to use for say output...I know how to perform generic error trapping, but would like more versatility in using success/error output messages.

Replies are listed 'Best First'.
Re: Grabbing tokens
by broquaint (Abbot) on May 13, 2003 at 14:08 UTC
    A simpler option may be backticks
    my $res = (split ' ', `net use J\: \\\\$hostname\\c\$`)[2];
    See. `STRING` under Regexp Quote-Like Operators in perlop for more info.


Re: Grabbing tokens
by cbro (Pilgrim) on May 13, 2003 at 14:12 UTC
    I ran a test and verified that this will work:
    #!/usr/local/bin/perl $prg = ""; $ret_var = <`$prg`>; print "\n\n$ret_var\n\n";
    I got what echoed back in $ret_val. As well, I verified that the command that executed (`/bin/cp file1 file2`) worked too. So, the execution was not hindered, and the output of was brought back into my Perl program for parsing. This should do it.
      Are you sure? I tried and it failed. To my understanding
      $ret_var = <`$prg`>;
      would first run $prg because of the backticks, and then try to handle this output as a filehandle.
      $ret_var = `$prg`;
      will do what you stated.
        Yes, I'm sure...otherwise, I would not have stated that I tested it.
        Here are the exact programs so that you can verify. I will admit, I did mistype one thing...I forgot to put the './' in the $prg scalar:
        ###Begin Perl### #!/usr/local/bin/perl $prg = "./"; $ret_val = <`$prg`>; print "\n\n$ret_val:From Perl Not BASH Script\n\n"; ###End Perl#### ###Begin Bash### #!/usr/local/bin/bash `/bin/cp / /` echo Success ###End Bash###
        What is printed:

        Success:From Perl Not BASH Script

        Try these scripts Skeeve. I wouldn't just claimed to have tested something...
Re: Grabbing tokens
by kilinrax (Deacon) on May 13, 2003 at 14:10 UTC
    Use backticks (' `command` ' or ' qx|command| '), like this:
    bash-2.05$ perl -le '$_ = qx[uname -a]; split; print join q[ :: ], @_' SunOS :: prdapp5 :: 5.8 :: Generic_108528-15 :: sun4u :: sparc :: SUNW +,Ultra-Enterprise
    Apologies for using a different command, but I don't have access to ActivePerl right now :}
Re: Grabbing tokens
by edan (Curate) on May 13, 2003 at 14:11 UTC

    You could try (untested):

    my $output; chomp($output = `net use J: \\\\$hostname\\c\$`); if ($output =~ /successfully/) { print "You're in luck!\n"; }

    If you're really partial to the third token idea, then split is your friend...


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://257732]
Approved by broquaint
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2022-05-25 23:55 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (91 votes). Check out past polls.