Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Cannot find input arguments to program called with "system"

by pktrain (Acolyte)
on Jul 06, 2011 at 05:52 UTC ( #912913=perlquestion: print w/ replies, xml ) Need Help??
pktrain has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I'm writing a Perl script to run an external program on every file in a directory. This program converts files from one format to another. Here's the deal...

When I run the program from the command line, everything works as it should:

computer.name % /path/program /inpath/input.in /outpath/output.out converting: /inpath/input.in to /outpath/output.out computer.name %

Here's the code I wrote to convert all files in a directory (listed in "file_list.txt"):

#!/usr/bin/perl -w use warnings; use diagnostics; use FileHandle; use File::Copy; # Set simulation parameters and directories @test_dates = ("20110414"); $listfile = "file_list.txt"; $execname = "/path/program"; foreach $date (@test_dates) { # Set/make directories $obs_file_dir = "inpath"; $pred_file_dir = "outpath"; mkdir "$pred_file_dir", 0755 unless -d "$pred_file_dir"; # Read input file names to array $obs_file_list = $obs_file_dir . $listfile; open(DIR, $obs_file_list) or die "Could not open file!"; @obs_files = <DIR>; close(DIR); # Convert and save files foreach $file (@obs_files) { $file =~ s/(\*)//g; $infile = $obs_file_dir . $file; $outfile = $pred_file_dir . $file; $outfile =~ s/nid/cdf/g; print $infile . "\n"; @arg_list = ($execname, $infile, $outfile); system(@arg_list); } }

The output shows me the following error for every file in the list:

computer.name % perl_script_name.pl /inpath/input.in converting: /inpath/input.in to /outpath/output.out unable to find /inpath/input.in stat status=-1 error while processing the product

I verified every file is in the proper place and have no idea why I am getting this error. Your advice is greatly appreciated!

Comment on Cannot find input arguments to program called with "system"
Select or Download Code
Re: Cannot find input arguments to program called with "system"
by philipbailey (Chaplain) on Jul 06, 2011 at 07:07 UTC

    You should chomp $file. It otherwise contains a newline character at the end.

      Thank you! Using chomp() was the solution...
Re: Cannot find input arguments to program called with "system"
by Khen1950fx (Canon) on Jul 06, 2011 at 07:07 UTC
    Your biggest problem was not declaring your lexical variables with 'my'. I fixed that and made a few changes.
    #!/usr/bin/perl use strict; use warnings; my(@test_dates) = '20110414'; my $listfile = '/root/Desktop/Log.txt'; foreach my $date(@test_dates) { my $obs_file_dir = 'inpath'; my $pred_file_dir = 'outpath'; mkdir "$pred_file_dir", 0755 unless -d "$pred_file_dir"; my $obs_file_list = $listfile; open( DIR, '<', $obs_file_list ) or die $!; my(@obs_files) = <DIR>; close(DIR); foreach my $file(@obs_files) { $file =~ s/(\*)//g; my $infile = $obs_file_dir . $file; my $outfile = $pred_file_dir . $file; $outfile =~ s/nid/cdf/g; print $infile . "\n"; } }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://912913]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2014-07-14 09:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (257 votes), past polls