Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Grabbing tokens

by kidwahoo94 (Novice)
on May 13, 2003 at 13:56 UTC ( [id://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.
    HTH

    _________
    broquaint

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 = "testers.sh"; $ret_var = <`$prg`>; print "\n\n$ret_var\n\n";
    I got what testers.sh echoed back in $ret_val. As well, I verified that the command that testers.sh executed (`/bin/cp file1 file2`) worked too. So, the execution was not hindered, and the output of testers.sh was brought back into my Perl program for parsing. This should do it.
    HTH,
    Chris
      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 = "./testers.sh"; $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 /wantstats.sh /newstats.sh` 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...

    --
    3dan

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-25 03:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found