开发者

Cannot find argument passed to program called using Perl "system" command

开发者 https://www.devze.com 2023-03-18 11:57 出处:网络
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...

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/in/out/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. Why can't the files be found? When I manually pass the arguments using the command line, no problem. When I pass the arguments through a variable via a system call, they can't be found even though the path and file names are correct.

Your advice is greatly appreciated!


Your list of files (@obs_files) comes from reading in a file via @obs_files = <DIR>;

When you do that, each element of array will be a line from a file (e.g. directory listing), with the line being terminated by a newline character.

Before using it, you need to remove the newline character via chomp($file).

Please note that s/(\*)//g; does NOT remove that trailing newline!

0

精彩评论

暂无评论...
验证码 换一张
取 消