Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

shell read of val

by Anonymous Monk
on May 16, 2018 at 16:23 UTC ( #1214662=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi , I am trying to get a shell script into perl.
command1|grep val | read val; if [[$val -eq 1]] then; do .. ; else ..; + fi;
But in perl when I have it in
$x = `command1|grep val | read val; echo $val; if [[$val -eq 1]] then; + do .. ; else ..; fi;`
In perl it doesnt echo val.

Please advice.

Thank you Intern guy

Replies are listed 'Best First'.
Re: shell read of val
by roboticus (Chancellor) on May 16, 2018 at 18:15 UTC

    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.

      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.
Re: shell read of val
by karlgoethebier (Monsignor) on May 16, 2018 at 18:34 UTC

    See also

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

      Thank you friends I was able to resolve the issue.
        "...I was able to resolve the issue."

        Very nice. How did you do it?

        Best regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

        perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2018-11-13 02:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My code is most likely broken because:
















    Results (149 votes). Check out past polls.

    Notices?