http://qs321.pair.com?node_id=1214677


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.