为什么从Perl运行时没有设置批处理文件的环境变量? [英] Why aren't a batch file's environment variables set when run from Perl?

查看:178
本文介绍了为什么从Perl运行时没有设置批处理文件的环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从perl脚本中调用bat文件,如下所示:

I am trying to invoke a bat file from a perl script as follows:

 system("call D:/SIP/run_$file_idx.bat");

但是我观察到bat中的环境变量无法解决。
如果我从命令提示符处分开运行bat文件,它可以工作。

However I observe that the environment variables in the bat fail to get resolved. If I run the bat file separately from the command prompt it works.

system()创建一个新的环境并执行bat文件吗?
我错过了什么?

Does system() create a new environment and execute bat file in that? What is that I am missing?

if (($ENV{'IPSL_RUN_FLAG'}) eq "TRUE") {

my $Book = $Excel->Workbooks->Open(
    "$ENV{'IPSL_TESTCASES_PATH'}IPSLFeatureLoadRunResults.xls"); 

# Make transparent where the IPSL tarball is installed. 
# Have ControlPanel save results here.

# You can dynamically obtain the number of worksheets, rows, and columns
# through the Excel OLE interface.  Excel's Visual Basic Editor has more
# information on the Excel OLE interface.  Here we just use the first
# worksheet, rows 1 through 4 and columns 1 through 3.

# select worksheet number 1 (you can also select a worksheet by name)

my $count=0;

my $Sheet = $Book->Worksheets("LOADDATA");
my $tmp=0;


foreach my $row (13..776) {
    foreach my $col (17..17) {
        if(($Sheet->Cells($row,$col)->{'Value'} eq "Failed") || 
            ($Sheet->Cells($row,$col)->{'Value'} eq "No Run") ) {
            $cnt_of_current_rerun_testcases++;
            foreach my $col (18..18) {
                # skip empty cells
                next unless defined $Sheet->Cells($row,$col)->{'Value'};
                my $a = $Sheet->Cells($row,$col)->{'Value'};

                my $i = index($a, 'run');  
                $a = substr($a, 0, $i); #remove runTest*  

                print OUT "\n";
                if($count == 0) {
                    print OUT "\nREM "; 
                    print OUT  "*" x 100; 
                    print OUT "\n";
                    print OUT "\ntaskkill /F /FI \"USERNAME eq %USERNAME%\" /IM ips32.exe";
                    print OUT "\ntaskkill /F /FI \"USERNAME eq %USERNAME%\" /IM ipsldb.exe";
                    print OUT "\ntaskkill /F /FI \"USERNAME eq %USERNAME%\" /IM ipsltiu.ex\n";    
                }

                print OUT "c:\n";            
                print OUT "\ncd ";

                $a =~ s/%I/\"%I/g;
                $a=~s/H%/H%\"/g;

                print OUT " $a\n";

                print OUT "\n";
                print OUT "CALL run_SubscribeFeatureOnHIQ.bat";
                print OUT "\n";

                print OUT "sleep 10\n";

                print OUT "\ncd ";
                print OUT " $a\n";
                print OUT "\n";

                print OUT "CALL ";
                $i=$Sheet->Cells($row,$col)->{'Value'};
                $i=~ s/%I/\"%I/g;
                $i=~s/H%/H%\"/g;
                print OUT $i;
                #print OUT  $Sheet->Cells($row,$col)->{'Value'};
                print OUT "\n";

                $count++;
                if($count == $no_simul_tcases) {
                    $sleep_cnt++;
                    print OUT "echo Going for sleep $sleep_cnt\n";
                    print OUT "SLEEP 300";
                    print OUT "\n";
                    $count=0;
                }
            }
        }
    }
}

print OUT "\ntaskkill /F /FI \"USERNAME eq %USERNAME%\" /IM ips32.exe";
print OUT "\ntaskkill /F /FI \"USERNAME eq %USERNAME%\" /IM ipsldb.exe";
print OUT "\ntaskkill /F /FI \"USERNAME eq %USERNAME%\" /IM ipsltiu.ex\n";         
print OUT "\nset IPSL_RUN_FLAG=TRUE";
close OUT ;
system(\"start $ENV{'IPSL_TESTCASES_PATH'}SIP\\run_$file_idx.bat\");

批处理文件是:

taskkill /F /FI "USERNAME eq %USERNAME%" /IM ips32.exe
taskkill /F /FI "USERNAME eq %USERNAME%" /IM ipsldb.exe
taskkill /F /FI "USERNAME eq %USERNAME%" /IM ipsltiu.ex
c:

cd  "%IPSL_TESTCASES_PATH%"SIP\TestCASE\FEATURESINT\INT_cfSRS\INT_cfSRS_cfSERRNG\

CALL run_SubscribeFeatureOnHIQ.bat
sleep 10


cd  "%IPSL_TESTCASES_PATH%"SIP\TestCASE\FEATURESINT\INT_cfSRS\INT_cfSRS_cfSERRNG\

CALL "%IPSL_TESTCASES_PATH%"SIP\TestCASE\FEATURESINT\INT_cfSRS\INT_cfSRS_cfSERRNG\runTest_SRS_INT_SERRNG.bat

可能的解决方法:

通过使用exec并将ENV专门写入bat文件,给了我一个解决方法: p>

By using exec and specifically writing the ENV into bat file gave me a workaround:

print OUT map { "set $_=$ENV{$_}\n" } 
          qw( path USERPROFILE USERNAME ProgramFiles ComSpec APPDATA );

仍然可以看到system()的问题。我在回答者中尝试了Sinan的建议。

The issue is still seen with system() though. I tried Sinan's suggestion in the answerers..

推荐答案

子进程继承其父进程的环境。蝙蝠文件应该与Perl脚本具有相同的环境。如果您没有正确设置环境,您的蝙蝠文件将不会看到。

Child processes inherit the environment of their parent. The bat file should have the same environment as the Perl script. If you haven't setup the environment correctly, your bat file won't see it.

您可以扩展您的答案以显示您的测试用例,类似于Sinan做了什么?虽然你在你的评论中说,代码很简单,但这并不是真的。告诉我们您拥有的完整Perl程序和您拥有的完整的蝙蝠文件。不要让我们猜测或做很多工作来弄清楚你在做什么。

Can you expand your answer to show your test case, similar to what Sinan has done? Although you say in your comment that the code is simple, that is never true. Show us the complete Perl program you have and the complete bat file that you have. Don't make us guess or do a lot of work to figure out what you are doing.

更新:现在你提供了一些代码,开始切割零件,使最简单的测试案例仍然显示问题。当您正在调查这些问题时,请停止在大脚本上工作,并编写一个脚本,以执行您认为的问题。这样,你可以将它与其他一切事情隔离开来。

Update: Now that you've provided some code, start cutting parts out of it to make the simplest test case that still shows the problem. When you are investigating these sorts of problems, stop working on the big script and write a little script that exercises the problem you think you're having. That way, you isolate it from everything else you might be messing up.

这篇关于为什么从Perl运行时没有设置批处理文件的环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆