找不到傳遞給使用 Perl“系統”命令調用的程序的參數 (Cannot find argument passed to program called using Perl "system" command)


問題描述

找不到傳遞給使用 Perl“系統”命令調用的程序的參數 (Cannot find argument passed to program called using Perl "system" command)

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!

‑‑‑‑‑

參考解法

方法 1:

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!

(by EvanDVK)

參考文件

  1. Cannot find argument passed to program called using Perl "system" command (CC BY‑SA 3.0/4.0)

#system #perl #arguments #Command






相關問題

asp.net c# sistem login (asp.net c# login system)

系統函數的彙編代碼(iPhone) (Assembly code to system functions (iPhone))

如何跟踪系統依賴關係? (How to track System Dependencies?)

使用 system() 甚至 passthru() 從 php 運行 python 腳本不會產生任何輸出。為什麼? (Running a python script from php with system( ) or even passthru( ) produces no output. Why?)

求解線性方程 (Solving a linear equation)

C++ 中的 System() 調用及其在編程中的作用 (System() calls in C++ and their roles in programming)

找不到傳遞給使用 Perl“系統”命令調用的程序的參數 (Cannot find argument passed to program called using Perl "system" command)

如何顯示所有用戶表中的所有列/字段? (How to display all columns/fields in all user tables?)

系統文件夾中的庫未加載正確的語言 (Libraries from system folder does not load correct language)

為什麼系統不返回主值? (Why system doesn't return main's value?)

通知未顯示,因為服務或警報管理器已被終止 (Notification not showing because service or alarm manager is killed off)

通過 C++ 調用系統不一致失敗 (Calling the system through C++ inconsistently fails)







留言討論