By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,541 Members | 1,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,541 IT Pros & Developers. It's quick & easy.

invoking unix command in perl

blackgoat
P: 33
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:

Expand|Select|Wrap|Line Numbers
  1. my $var = 'command';
  2. 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
Mar 5 '10 #1
Share this Question
Share on Google+
8 Replies


jkmyoung
Expert 100+
P: 2,057
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)
Mar 5 '10 #2

blackgoat
P: 33
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
Mar 6 '10 #3

jkmyoung
Expert 100+
P: 2,057
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?
Mar 8 '10 #4

blackgoat
P: 33
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!
Mar 9 '10 #5

Expert Mod 100+
P: 589
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
Mar 9 '10 #6

blackgoat
P: 33
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:
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
  3. use CGI;
  4.  
  5. $var = `COMMAND`;
  6. my ($var1) = $var =~ m/pattern/;
  7.  
  8. print "Content-type:text/html\n\n
  9.  
  10. <html>
  11.  
  12. <head>
  13.  
  14. <title>TITLE</title>
  15.  
  16. <script language='Javascript'>
  17.  
  18. function validate_required(field,alerttxt)
  19.         {
  20.         with (field)
  21.                 {
  22.                 if (value==null||value==\"\")
  23.                         {
  24.                         alert(alerttxt);return false;
  25.                         }
  26.                 else
  27.                         {
  28.                         return true;
  29.                         }
  30.                 }
  31.         }
  32.  
  33. function validate_form(thisform)
  34.         {
  35.         with (thisform)
  36.                 {
  37.                 if (validate_required(rundir,\"Runpath must be filled out!\")==false)
  38.                         {
  39.                         rundir.focus();return false;}
  40.                         }
  41.                 }
  42. </script>
  43.  
  44. </head>
  45.  
  46. <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
  47.  
  48. # I want to print the output of 'var1' here
  49.  
  50. <h2><div align=\"center\"><font color='CC3300'>HEADING</font></div></h2><br><br>
  51.  
  52. <div align=\"center\">
  53. <table cellpadding=\"10\">
  54.         <tr>
  55.                 <td>
  56.                         <font color='CC3300'><b>Enter rundir path</b>:</font>
  57.                 </td>
  58.  
  59.                 <td>
  60.                         <input type=text name=rundir size=50 onChange=\"valid_data(this.name)\">
  61.                 </td>
  62.         </tr>
  63.  
  64.         <tr>
  65.                 <td>
  66.                         <font color='CC3300'><b>Select Version</b>:</font>
  67.                 </td>
  68.  
  69.                 <td>
  70.                          OPTIONS
  71.                 </td>
  72.         </tr>
  73.  
  74.  
  75.         <tr>
  76.                 <td>
  77.                         <font color='CC3300colorcolor'><b>Group</b>:</font>
  78.                 </td>
  79.                 <td>
  80.                         OPTIONS
  81.                 </td>
  82.         </tr>
  83.  
  84.         <tr>
  85.                 <td></td>
  86.  
  87.                 <td>
  88. <input type=\"submit\" value=\"Submit\" />
  89.                 </td>
  90.         <tr>
  91.                 <td></td>
  92.  
  93.                 <td>
  94.                          <form name=user_log action=\"\" method=POST>
  95.                                 <input type=submit name=submit value=\"User Log\">
  96.                         </form>
  97.                 </td>
  98.         </tr>
  99. </table>
  100.  
  101. </div>
  102.  
  103. </body>
  104.  
  105. </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.
Mar 9 '10 #7

blackgoat
P: 33
hey! do I need to use @ARGV in some way? how??
Mar 9 '10 #8

Expert Mod 100+
P: 589
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:
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
  6. use CGI;
  7.  
  8. my $cgi = CGI->new;
  9. print $cgi->header,
  10.       $cgi->start_html('test');
  11.  
  12. warningsToBrowser(1);
  13.  
  14. # output the raw command that you intend to execute
  15. # execute the command
  16. # output the results
  17. # apply your regex
  18. # output the new results
  19.  
  20. print $cgi->end_html;
Mar 9 '10 #9

Post your reply

Sign in to post your reply or Sign up for a free account.