Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: shell read of val

by roboticus (Chancellor)
on May 16, 2018 at 18:15 UTC ( [id://1214677]=note: print w/replies, xml ) Need Help??


in reply to shell read of val

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.

Replies are listed 'Best First'.
Re^2: shell read of val
by morgon (Priest) on May 16, 2018 at 18:40 UTC
    I'll add two things to roboticus' excellent treatment:

    Firstly, be careful with backticks when you want your your script to run on different systems as it always runs a "system shell" and that may not be the same shell on different systems.

    And secondly there is another syntax for backticks, which is qx. The advantage of qx is that you can choose your own delimiters, so e.g.

    my $result = `command`;
    can also be written as
    my $result = qx| command |;
    which I find nicer, though your taste my of course be different.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2024-03-28 10:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found