windows - 查找在批处理脚本中运行时发生的位置或行错误?
问题描述
我想知道批处理脚本发生错误的错误行号,并且我想在批处理脚本中打印整行?
set t=%date%_%time%
set x=FORD_DLCM_T6_FTC
set a="%m%\%x%"
cd /d "C:\PRQA\PRQA-Framework-2.1.2\common\bin"
qacli admin --set-license-server 5055@00.00.0.0 || goto :error
qacli admin --debug-level DEBUG || goto :error
goto :EOF
:error
set remark= %ERRORLEVEL%
set status= Failure
echo ProjectName: %x%
echo Status: %status%
echo Remark: %remark%
echo Date: %t%
echo %x%,%status%,traceback(),%t% > "C:\Test\Build.csv"
echo Failed with error #%errorlevel%.
exit /b %errorlevel%
谁能帮我吗?
解决方案
jeb 已经描述了搜索正确行的工作原理。如果发生错误,将触发调用,参数是一个唯一的字符串和一个偏移量,它指向正确行的缺失行数。Findstr 查找明确无误的字符串并打印该行号。有了解决方案,这又是正确的。
我整理了一些宏来表明还有其他方法可以读取正确的行。
@echo off
setlocal
set prompt=$g$s
call :setAllmacros
(call)
rem begin in echo off - what line is it please ?
%ID-1:##==rem?%
(call )
%ID-1:##==call%
set/a+
%ID-1:##==001% call :ErrorLogging Line:%%L Errorlevel:%errorlevel%
echo 2
(call) || %ID-0:##==2% call :Errorlogging Line:%%L Errorlevel:%%errorlevel%%
echo %errorlevel%
find /n || %ID-0:##==003% call :Errorlogging Line:%%L Errorlevel:%%errorlevel%%
%ID-0:##==008%
pushG 556
%ID-1:##==004% call :ErrorLogging Line:%%L Errorlevel:%errorlevel%
%Line#print%:33=="
pause
exit /b
:errorlogging
@echo on
rem %*
@echo off
exit /b
:setAllmacros
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
(set ^"lf=^
%== define LF + CR ==%
)
for /f %%i in ('copy /z "%~f0" nul') do set "CR=%%i"
rem Prints the fullline itself with LineNumber into stderr
rem usage: %Line#print%:UniqueID"
rem command || %Line#print%:01"
set "Line#print= @ <"%~f0" >&2 find /n "%%Line#print%%"
rem Read the fullline BEFORE into Variables with LineNumber; do something ...
rem usage: command 1 line before
rem %ID-1:##==01% [command %%L ... %%M ...]
set ID-1= if errorlevel 1 for /f "usebackQtokens=1*delims=:" %%L in (^
`cmd /v /c ^"findstr /n /r /c:"..*^!CR^!*^!LF^!.*ID-1:.#^=^##%%" "%~f0"^"`) do ^>^&2 echo BatchLine: %%L -- %%M ^&
rem Read the fullline itself into Variables with LineNumber; do something ...
rem usage: command || %ID-0:##==01% [command %%L ... %%M ...]
set ID-0= @ for /f "tokens=1*delims=:" %%L in ('findstr /n /r /c:"ID-0:.#=##%%" "%~f0"') do ^>^&2 echo BatchLine: %%L -- %%M ^&
exit /b
希望能帮助到你
菲尔
推荐阅读
- javascript - 从一个页面调用会话到另一个页面并将其包含在 javascript 中
- forms - 如何在打开表单之间自由点击。VBA
- wordpress - 如何将 wordpress 插件复制到三个或更多
- continuous-integration - 持续集成 - 如何使用来自 2 个构建但仅由 1 个构建触发的工件
- database - EF 迁移“基线脚本”功能
- html - 如何在不滚动主体滚动条的情况下在带有滚动条的 div 中使用 focus() 来聚焦 id
- python - 输入包含 NaN、无穷大或对于 dtype('float64') 错误而言太大的值,但数据集中没有值
- java - 在eclipse中导出项目只导出.classpath和.project
- reactjs - 具有大量图像的视图的 React Native 渲染性能
- ios - iOS SDK Shopify:更改集合中产品的排序顺序