invoking unix command in perl

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blackgoat
    New Member
    • Jan 2010
    • 33

    invoking unix command in perl

    I have to run a command in unix. And I have to match this expression from whatever output I get. I tried doing the following:

    Code:
    my $var = 'command';
    my ($exp) = $var =~ m/pattern/;
    The same is followed by a html script in which the value of $exp must appear. But I am unable to do so...

    Can u pls help! what is the best way to do this??

    Thanks

    BG
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Are you using the right punctuation?

    It looks like you're using apostrophes ' instead of backticks ` (under ~, and next to the 1 on most keyboards)

    Comment

    • blackgoat
      New Member
      • Jan 2010
      • 33

      #3
      yeah I know .... I fixed that bug but it still doesn't work! is that the ony mistake? or am I doing some other mistake as well??

      BG

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        Have you tried outputting var? Eg, are you getting output?
        (It could be the case that the output pipes to std error, instead of std out.)

        Also are you sure your regex is correct? What's in the value of your pattern?

        Comment

        • blackgoat
          New Member
          • Jan 2010
          • 33

          #5
          The regex is working otherwise.. when I try it in a perl script editor separately by giving a dummy paragraph. Its only when I try embedding it in my html script that I have trouble. Strangely enough I get no errors on debugging my html file... and yet the output of my regex is totally ignored when I run it!

          Comment

          • RonB
            Recognized Expert Contributor
            • Jun 2009
            • 589

            #6
            You haven't provided us enough info to help you.

            Please post:
            1) your entire script
            2) what input is it receiving
            3) what you expect it to do
            4) what it is doing that it shouldn't
            5) what it is not doing that it should
            6) what errors/warnings are you recieving

            Comment

            • blackgoat
              New Member
              • Jan 2010
              • 33

              #7
              i cant post my entire script coz it has a number of interlinked scripts plus some of the data is confidential. however i will give u the script in which i must incorporate this command:
              Code:
              #!/usr/bin/perl -w
              use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
              use CGI;
              
              $var = `COMMAND`;
              my ($var1) = $var =~ m/pattern/;
              
              print "Content-type:text/html\n\n
              
              <html>
              
              <head>
              
              <title>TITLE</title>
              
              <script language='Javascript'>
              
              function validate_required(field,alerttxt)
                      {
                      with (field)
                              {
                              if (value==null||value==\"\")
                                      {
                                      alert(alerttxt);return false;
                                      }
                              else
                                      {
                                      return true;
                                      }
                              }
                      }
              
              function validate_form(thisform)
                      {
                      with (thisform)
                              {
                              if (validate_required(rundir,\"Runpath must be filled out!\")==false)
                                      {
                                      rundir.focus();return false;}
                                      }
                              }
              </script>
              
              </head>
              
              <body style=\"background-color:FFF0F5\"><form name='form1' action='http://cdsiweb/cgi-bin/elc/random_num.pl' method='POST' onsubmit=\"return validate_form(this)\">&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
              
              [B]# I want to print the output of 'var1' here[/B]
              
              <h2><div align=\"center\"><font color='CC3300'>HEADING</font></div></h2><br><br>
              
              <div align=\"center\">
              <table cellpadding=\"10\">
                      <tr>
                              <td>
                                      <font color='CC3300'><b>Enter rundir path</b>:</font>
                              </td>
              
                              <td>
                                      <input type=text name=rundir size=50 onChange=\"valid_data(this.name)\">
                              </td>
                      </tr>
              
                      <tr>
                              <td>
                                      <font color='CC3300'><b>Select Version</b>:</font>
                              </td>
              
                              <td>
                                       OPTIONS
                              </td>
                      </tr>
              
              
                      <tr>
                              <td>
                                      <font color='CC3300colorcolor'><b>Group</b>:</font>
                              </td>
                              <td>
                                      OPTIONS
                              </td>
                      </tr>
              
                      <tr>
                              <td></td>
              
                              <td>
              <input type=\"submit\" value=\"Submit\" />
                              </td>
                      <tr>
                              <td></td>
              
                              <td>
                                       <form name=user_log action=\"\" method=POST>
                                              <input type=submit name=submit value=\"User Log\">
                                      </form>
                              </td>
                      </tr>
              </table>
              
              </div>
              
              </body>
              
              </html>"
              I assure you that the regex pattern is working fine as I have tested it. Its only I can not embedded in the script.

              The above mentioned COMMAND is to be run in unix that results in some textual output from which the pattern is recognised.

              Comment

              • blackgoat
                New Member
                • Jan 2010
                • 33

                #8
                hey! do I need to use @ARGV in some way? how??

                Comment

                • RonB
                  Recognized Expert Contributor
                  • Jun 2009
                  • 589

                  #9
                  Before I point out everything that you're doing wrong in that code, it would be better for you to create a short and complete test script that demonstrates the problem.

                  Your test script, and in fact EVERY Perl script you write should include the warnings and strict pragmas. So, your test script should look like this:
                  Code:
                  #!/usr/bin/perl
                  
                  use strict;
                  use warnings;
                  use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
                  use CGI;
                  
                  my $cgi = CGI->new;
                  print $cgi->header,
                        $cgi->start_html('test');
                  
                  warningsToBrowser(1);
                  
                  # output the raw command that you intend to execute
                  # execute the command
                  # output the results
                  # apply your regex
                  # output the new results
                  
                  print $cgi->end_html;

                  Comment

                  Working...