Solved

Can't get system command response to return?

Posted on 2003-11-30
8
455 Views
Last Modified: 2008-03-17
I want to have the results of the following system command be loaded into a scalar but it does not seem to do it either way I have tested it.

Here is the code:

----------code-----------------------
sub interpret {
   my $test = `perl -c syn_check`;
   #chomp(my $test = system('perl -c syn_check'));
   $info = "Checking script syntax.";
   open (IN, ">syn_check");
   print IN $t->get("1.0", "end");
   close(IN);
   $info = "Syntax Tested\!";
   if ($test eq "syn_check syntax OK") {
      $status->insert("end", "Syntax passed!\n");
   }else{
      print "\$test equals => " . $test . "\n";
      $status->insert("end", $test);
   }
}
--------------------end code----------------------

All I can get to return is the function status (either a 1 or zero) and not the actual text returned from the perl interpreter. Is there anyway I can make this work? I addes the print statement in the else section to see what the value of $test was at that point so I could see why the script was jumping to that section every time.
0
Comment
Question by:ZiaTioN
8 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 9846193
> my $test = `perl -c syn_check`;
should work

> if ($test eq "syn_check syntax OK") {
most likely does not work ('cause $test ends with \n in most cases)
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 9846385
You could try changing:

   if ($test eq "syn_check syntax OK") {

to this:

   if ($test =~ /syn_check syntax OK/i) {

or this:

   if ($test =~ /syntax OK/i) {
0
 
LVL 5

Author Comment

by:ZiaTioN
ID: 9847151
Yeah I tried using the match (=~) function also but had same issue. None of it seems to work.
0
 
LVL 5

Author Comment

by:ZiaTioN
ID: 9847209
It appears that $test is blank when I try to find out what content is in the variable. Why would $test be blank?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Author Comment

by:ZiaTioN
ID: 9847297
Hmm.. I also tried the following to make sure something was being returned:

------code-------------
my $test = system('perl -c syn_check > results.txt');
----end code----------

The file results.txt is blank after the script is ran. It is almost like seeing how envoking the perl interpretor is not a direct system call or command that the script is not capturing the response from the interpretor. Would calling the perl interpretor send a response back through a different median than a normal system call like "dir" would? This is the only thing I can think of. Is this a bug in perl itself?

0
 
LVL 5

Author Comment

by:ZiaTioN
ID: 9847361
Ok it does seem that there is no value being assigned to the variable $test. Maybe because of the reasons in my last post. Here is the newest revision to my code that proves this finding.

---------code--------------------
sub interpret {
   $info = "Checking script syntax.";
   open (IN, ">syn_check");
   print IN $t->get("1.0", "end");
   close(IN);
   #my $dir = system('dir > dir.txt');
   my $test = `perl -c syn_check`;
   #my $test2 = system('perl -c syn_check > results.txt');
   $info = "Syntax Tested\!";
   if ($test) {
      if ($test eq "syn_check syntax OK") {
          $status->insert("end", "Syntax passed!\n");
      }else{
         #print "\$test equals => " . $test . "\n";
         $status->insert("end", $test);
      }
   }else{
      $status->insert("end", "There was an error while receiving response from interpretor!\n");
   }
}
--------end code-------------------------

The script always jumps to the "else" section meaning that $test has no value.
0
 
LVL 20

Accepted Solution

by:
jmcg earned 100 total points
ID: 9847498
When you invoke perl with the "-c" flag, the message with "syntax OK" is printed to STDERR, not to STDOUT. That's why it is not appearing in your $test string or in the results.txt file.

Try doing it with file descriptor explicitly redirected; this snippet works okay for me:

   $test = `perl -c syn_check 2>&1`;
   $info = "Checking script syntax.";
   if ($test eq "syn_check syntax OK\n") {
      print "Syntax checked OK\n";
   }else{
      print "\$test equals => ", $test, "\n";
   }

I suspect this approach may not work if you're not on Unix/Linux, since it depends on shell syntax being recognized.
0
 
LVL 5

Author Comment

by:ZiaTioN
ID: 9848056
That was exactly it man! Good call. Oh and yes your method does work on Win32 also. This was probably one of the most annoying and ongoing problems I have had working with any perl script ever. Thanks for your help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now