perl - 可以在 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 将启动,但我看不到哪一行执行此操作?
解决方案
它正在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.
推荐阅读
- winapi - win32com .Formula = "" 功能不起作用?
- formula - 当我想在谷歌应用程序脚本表中编写相应的公式时出现错误(解析错误)
- sql - 限制 regexp_substr 表达式的第 2 部分
- javascript - Javascript数组数学计算结果
- javascript - 必应地图 - 无法在 clusteredPinCallback 中设置和访问图钉属性
- python - 子功能和功能练习
- python - Tkinter 不确定的进度条被线程卡住了
- typescript - 模具是否使用 tsconfig.json?
- git - 如何从 github REST api 获取给定 blob sha 的提交 sha
- python - 有没有一种方便的方法来自动化 HomeAssistent (Hass.io) 备份 / snapsnots?