Solved

Can't get system command response to return?

Posted on 2003-11-30
8
454 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
Comment Utility
> 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
Comment Utility
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
Comment Utility
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
Comment Utility
It appears that $test is blank when I try to find out what content is in the variable. Why would $test be blank?
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 5

Author Comment

by:ZiaTioN
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

772 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

13 Experts available now in Live!

Get 1:1 Help Now