perl中的Excute-dos命令
我在cgi文件中有一个简单的perl脚本,用于查找和替换dos中的文本,例如:perl中的Excute-dos命令,perl,cgi,dos,Perl,Cgi,Dos,我在cgi文件中有一个简单的perl脚本,用于查找和替换dos中的文本,例如: system("type data.txt | powershell -Command "$input | ForEach-Object { $_ -replace \"ID\", \"$id\" }" > new.txt"); 或 (我有Powershell和Cygwin) 当我运行这个脚本时,什么都没有发生! 但是如果我直接在cmd(windows)中键入这个命令,它就完成了! 我的问题是:如何在我的CG
system("type data.txt | powershell -Command "$input | ForEach-Object { $_ -replace \"ID\", \"$id\" }" > new.txt");
或
(我有Powershell和Cygwin)
当我运行这个脚本时,什么都没有发生!
但是如果我直接在cmd(windows)中键入这个命令,它就完成了!
我的问题是:如何在我的CGI文件中运行这些命令?通常情况下,这是由于shell和perl环境的环境设置不相同。它通常通过对命令使用显式路径名来修复 所以而不是
system("cat tmp/$id/index.html 's/ID/$id/g' a");
试一试
这可以在我的机器上工作。在cygwin中,您应该能够通过键入哪个命令
,将命令
替换为查找路径所需的命令,来确定任何给定命令的路径
我对Powershell的熟悉程度不如对Cygwin的熟悉,而且,我不知道您是否正在使用Cygwin perl。很可能您必须使用unix风格的路径来获取您的命令;如果您使用的是cygwin,那么可以使用类似于/cygdrive/c/window32/powershellcommand.exe
的东西。如果没有,您必须查找您的perl版本的文档,以获得确切的语法(或者其他人必须纠正我)
希望有帮助 open(my$fh,“,”new.txt“)或die“Error opening new.txt:$!”;
open(my $fh, "<", "data.txt") or die "Error opening data.txt: $!";
open(my $out, ">", "new.txt") or die "Error opening new.txt: $!";
my $id = 'NewId';
while (<$fh>) {
s/ID/$id/g;
print $out $_;
}
close $fh;
close $out;
my$id='NewId';
而(){
s/ID/$ID/g;
打印$out$;
}
收盘价$fh;
收尾美元;
copy tmp/$id/index.html的s/id/$id/g'a命令看起来像是假的。您可能想编写类似于copy tmp/$id/index.html`echo“$a”| sed's/id/$id/g`
?无论如何,在Perl中很容易进行替换;为什么您希望系统为您执行此操作?请注意,它是“cat”,而不是“copy”$我不明白为什么这个命令可以直接在cmd中运行,但是当我运行由Perl编写的CGI文件时,它无法解释为什么要从Perl中使用powershell?您有Perl.I系统(“/bin/cat tmp/$id/index.html的s/id/$id/g'a”);但和第一次一样。我不知道如何在cygwin中配置任何给定命令的路径。我从为unix系统编写的源代码编辑此文件,并希望在windows的IIS上运行它。这些命令让我感到困惑:(谢谢你的帮助
system("/bin/cat tmp/$id/index.html 's/ID/$id/g' a");
open(my $fh, "<", "data.txt") or die "Error opening data.txt: $!";
open(my $out, ">", "new.txt") or die "Error opening new.txt: $!";
my $id = 'NewId';
while (<$fh>) {
s/ID/$id/g;
print $out $_;
}
close $fh;
close $out;