Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to pass parameters to system call 2

Status
Not open for further replies.

hashem110251

Programmer
Feb 21, 2009
3
GB
I want to call md5sum.exe for a file and print it into another file, I have done
system("md5sum.exe $outputfile > $md_output") or die "system failed: $?";
but it doesnt like it and complaints:

system failed: 0 at C:/Documents and Settings/moac/workspace/lets_do_some_perl/test.pl line 16.
any help please?
 
are you passing full paths for $outputfile and $md_output?

Try using qx and get the output that way to.
@out = qx~md5sum.exe $outputfile > $md_output~ or die "system failed: $!\n";


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
I meant to add..

print "@out\n";

so you can see what's up.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Thanks travs69 for your redpond,
Actually everything is in the same directory (eg inputfile and output file and md5sume.exe). By the way, I used your command, as bellow:
my @out = qx~md5sum.exe $outputfile > $md_output~ or die "system failed: $!\n";
print (@out);
I still get "system failed: ".
I am very new to perl and not sure whether it was what you meant?
thanks
 
there should be more than just "system failed: ".

try getting the directory listing for the script just before calling md5sum

@dir=`dir`;
print join "\n", @dir;

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Thanks Paul,
I did apply your suggestion, but still I have just "system failed". And surprisingly the code does what I ask it to do.
Following is my code, do u c any thing wrong with that?


use strict;
use warnings;
#print "hi I am here\n";
my @pwms = ('M00041','M02001','M00890','M00999','M00100','M00064','M00991','M00001','M00231','M00011');
#print "@pwms", "\n";
my @sorted_pwms = sort(@pwms);
#print "@sorted_pwms";
my $outputfile = "sorted_pwms.txt";
my $md_output = "md5sum_of_pwm.txt";
open(FileHandle, ">", $outputfile) or die "can not open the $outputfile \n";
foreach my $element(@sorted_pwms){
print FileHandle $element,",";
}
close FileHandle;

#system("md5sum.exe $outputfile > $md_output") or die "system failed: $?";
my @out = qx~md5sum.exe $outputfile > $md_output~ or die "system failed: $!\n";
my @dir=`dir`;
print join "\n", @dir;
#print "@out";

exit;
#'md5sum.exe $outputfile > $md_output'
 
what files are you seeing being printed when you run the script - the output from the dir command?

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Found it (somewhat). Tried the following:
Code:
if (defined $ARGV[0]) {
   print "hi mom\n";
   exit 1 ; # also tried "exit 0" - neither worked
} else {
   my $out_file = "out_file.txt" ;
   my @out = qx~this_prog.pl an_arg > $out_file~ or die "SysFail\n";
   print @out ;
}
What's going on is that the program your calling is not returning a true value. Since you're redirecting the output, there's nothing to print. The following mod seemed to work
Code:
if (defined $ARGV[0]) {
   print "hi mom\n";
   exit 1 ; # This one triggers $? == 256 but no $!
            # since the program initiated the error and
            # not the OS
   exit 0 ; # This one left $? as zero
} else {
   my $out_file = "out_file.txt" ;
   my @out = qx~this_prog.pl an_arg > $out_file~
   die "SysFail $! $?\n" if ($?) ;
}

Due to it's simplicity, $out_file gets the results I was expecting in either case (the program actually works, I'm just exiting under different return codes.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top