从 Perl 运行批处理文件(Windows 中的 Activestate perl)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2907494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:31:48  来源:igfitidea点击:

Running a batch file from Perl (Activestate perl in Windows)

windowsperlactivestate

提问by Jithesh

I have a Perl program which does something like below:

我有一个 Perl 程序,它执行如下操作:

#!/usr/bin/env perl    
use strict;
use warnings;

my $exe = "C:\project\set_env_and_run.bat";

my $arg1 = "\\Server\share\folder1";    
my $arg2 = "D:\output\folder1";

my $cmd = "$exe \"$arg1\" \"$arg2\"";    
my $status = system("$cmd > c:\tmp\out.txt 2>&1");

print "$status\n";

I am calling this Perl code in an eval block. When invoked, i get the status printed as 0, but the batch file has not actually executed. What would be the reason for this? Any issue with the 'system' call coded above?

我在 eval 块中调用这个 Perl 代码。调用时,我将状态打印为 0,但批处理文件尚未实际执行。这是什么原因?上面编码的“系统”调用有什么问题吗?

Thanks, Jits

谢谢,吉茨

回答by mob

You need to escape your backslashes inside of double quotes.

您需要在双引号内转义反斜杠。

my $exe = "C:\project\set_env_and_run.bat";
...
my $status = system("$cmd > c:\tmp\out.txt 2>&1");

回答by justintime

Are you sure the bat file isn't running. I have taken your code, fixed up the paths that don't exist on my machine. I get it to call the batch file

你确定 bat 文件没有运行。我已经使用了你的代码,修复了我机器上不存在的路径。我得到它来调用批处理文件

echo In myrun  1=%1  2=%2

And it writes the following to the output file

并将以下内容写入输出文件

 In myrun  1="\Server\share\folder1"  2="D:\output\folder1"

回答by djangofan

I would say that you should define exe like this:

我会说你应该像这样定义 exe:

my $exe = "cmd.exe /c C:\project\set_env_and_run.bat";

回答by avinashkr

you could use

你可以用

 system ("start C:\project\set_env_and_run.bat");