Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

When you do something like this:

$x = `echo foo`;

You're starting up a shell, and running "echo foo" in the shell. Because you're using backticks, the output of the echo statement doesn't go to the console. Instead it's captured and placed in the $x variable so you can examine and/or operate on it:

$ cat t.pl use strict; use warnings; print "Let's do a command:\n"; my $x = `echo foo`; print "OK, command is done!\n\n"; print "Now let's see what we captured:\n<$x>\n";

When we run it, we see:

$ perl t.pl Let's do a command: OK, command is done! Now let's see what we captured: <foo >

So the simple answer to your question would simply be to add:

print $x;

to the end of your program.

However, if you're going to work in perl, it's presumably because you want to save yourself a bit of work. In that case, it will be better in the long run to do more of the work in perl, so the data is easily available to your code. While executing bits of bash scripts can and will work, it gets tricky to ensure that you have access to all the data you want. So let's take a look at how we can convert your command into perl.

First, if you assign the output of the backtick operator to an array, you'll capture all lines of output into that array. Look at the "Quote and Quote like Operators" section in the documentation (perldoc perlop) for more details:

my @results = `command`;

Now you've executed the command and have all the output from the command.

Next, you want check through the output and see if it contains a string, so you can use the grep function to sift through the results and keep only the lines containing the string you're asking for:

my @foo = grep /foo/ @results;

Then if the list is empty, you'll tell the user that the string they're looking for isn't available. Otherwise, you can tell them that it is:

if (0 == @foo) { print "The command didn't find 'foo'\n"; } else { print "Found 'foo'!\n"; }

So if you put it all together, you should have something like:

# Do the thing my @results = `command`; # Look for the stuff my @foo = grep /foo/ @results; # Did we find it? if (0 == @foo) { print "The command didn't find 'foo'\n"; } else { print "Found 'foo'!\n"; }

Since we captured the output, we can then also check for bar and/or baz, if we want, without re-running command, since we still have all the output of command in the @results variable.

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: shell read of val by roboticus
in thread shell read of val by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (3)
    As of 2021-01-28 01:59 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Notices?