I am trying to invoke a bat file from a perl script as follows:
system("call D:/SIP/run_$file_idx.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.
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\");
And the batch file is:
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
Possible workaround:
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 );
The issue is still seen with system() though. I tried Sinan's suggestion in the answerers..