首页 > 解决方案 > 可以在 perl 中打开命令执行 .bat 脚本吗?

问题描述

我有这个子程序启动一个 .bat 脚本,但我看不到哪一行是这样做的。

sub Traitement_Proc {

   foreach my $input (@_) {

    my $cmd = Fonctions_communes::ResolvEnvDos($input);
    my ($proc, $args);
    my $chExec;
    if ($cmd =~ /^\"/) {
        ($proc, $args) = $cmd =~ /^\"(.+?)\"(.*)/;
    } else {
        ($proc, $args) = $cmd =~ /^([^\s]+)(.*)/;
    }
    $chExec = File::Spec->catfile($::G_ParamTable{$::cstExecDir}, $proc);
    $chExec = File::Spec->rel2abs($chExec, File::Spec->curdir());
    $chExec = "\"".$chExec."\"" . $args;

    Fonctions_communes::PrintError("  PROC : "._("Execution of script")." <" . $chExec . ">");

    open PROC_OUT, $chExec." 2>&1"." & if ERRORLEVEL 1 exit/b 1"." |";
    while(my $output = <PROC_OUT>) {
        chomp($output);
        Fonctions_communes::PrintError(decode($Fonctions_communes::console_encoding,$output));
    }
    close(PROC_OUT);
    if ($? == 1) {
        Fonctions_communes::PrintError(_("The script [_1] ended in failure.",basename($chExec)).".\n");
        return 0;
    }
}

return 1;

}

里面$input有传入参数的bat文件的名称,$args在我的情况下没有,所以chExec变量是"C:\Users\anes.yahiaoui\Desktop\SPOOC_BD_TU_BD_XX_BD\tieme_RE_PCCA_BD_MAIN\RE_PCCA\BD\avantBDD\Gene_CSV\Proc\IMPORT_INV.bat".

当我调用此函数(Traitement_proc)时,我的 IMPORT_INV 将启动,但我看不到哪一行执行此操作?

标签: perl

解决方案


它正在open执行命令。两者都open(my $pipe, "shell_cmd |")执行open(my $pipe, "-|", "shell_cmd")一个shell命令,管道的另一端$pipe连接到它的STDOUT。

例如,

use strict;
use warnings;
use feature qw( say );

use Win32::ShellQuote qw( quote_system_string );

open(my $pipe, quote_system_string(@ARGV)." |")
   or die $!;

while (<$pipe>) {
   chomp;
   say("[$_]");
}

if (!close($pipe)) {
   die("Error waiting for child to exit: $!\n")      if $!;
   die("Child killed by signal ".( $? & 0x7F )."\n") if $? & 0x7F;
   die("Child exited with error ".( $? >> 8 )."\n")  if $? >> 8;
}

say("Child completed successfully.");
>a.pl perl -le"print for 1..5"
[1]
[2]
[3]
[4]
[5]
Child completed successfully.

推荐阅读