我正在處理代碼庫中的一個問題,其中 perl 腳本呼叫批處理腳本并根據退出代碼執行操作。在某些情況下,盡管exit /b 1執行了類似陳述句并且批處理腳本退出,但 perl 腳本將退出代碼視為 0。我已將其范圍縮小到以下示例。
這是 perl 腳本的最小版本:
#!/usr/bin/perl
print "calling bat with @ARGV\n";
qx(batscript.bat @ARGV);
my $exitcode = $? >> 8;
print "return code from bat is $exitcode \n";
這是批處理腳本的最小版本:
@echo OFF
setlocal enableextensions
if "%~1" == "err" (
echo "non-zero code requested"
exit /b 1
echo hello
)
endlocal
這就是我得到的:
c:\tmp>plscript.pl
calling bat with
return code from bat is 0
c:\tmp>plscript.pl err
calling bat with err
return code from bat is 0
如果我echo hello從批處理腳本中洗掉該行,它可以正常作業:
c:\tmp>plscript.pl
calling bat with
return code from bat is 0
c:\tmp>plscript.pl err
calling bat with err
return code from bat is 1
這是批處理如何運行和處理塊中的陳述句的錯誤嗎?最好不必重構批處理腳本,因為它很大并且有很多exit /b陳述句。
uj5u.com熱心網友回復:
根據 aschipfl 的建議,我最終決定使用CALL更改呼叫批處理腳本的方式。我喜歡這個解決方案,因為我不必修改批處理腳本(可以由 perl 腳本或用戶直接從 cmd.exe 視窗呼叫)。
所以基本上在 perl 腳本中,我將第三行從:
qx(batscript.bat @ARGV);
至:
qx(call batscript.bat @ARGV);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/533264.html
標籤:perl批处理文件命令
