Perl Monk, Perl Meditation | |
PerlMonks |
Re: shell read of valby roboticus (Chancellor) |
on May 16, 2018 at 18:15 UTC ( [id://1214677]=note: print w/replies, xml ) | Need Help?? |
When you do something like this:
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:
When we run it, we see:
So the simple answer to your question would simply be to add:
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:
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:
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:
So if you put it all together, you should have something like:
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 Section
Seekers of Perl Wisdom
|
|