usage: logit.sh [-h] [-p] [-i=n] [-s] <description>
example: logit.sh -p error -i 501 -s myscript.sh "failed to run the mount command"
下面是临时文件内容部分:
LGT_TEMP_FILE="$(mktemp --suffix .cmd)"
cat<<EOF>$LGT_TEMP_FILE
@echo off
set LGT_EXITCODE="$LGT_ID"
exit /b %LGT_ID%
EOF
unix2dos "$LGT_TEMP_FILE"
以下是在WEVL中创建事件的函数:
__create_event () {
local cmd="eventcreate /ID $LGT_ID /L Application /SO $LGT_SOURCE /T $LGT_PRIORITY /D "
if [[ "$1" == *';'* ]]; then
local IFS=';'
for i in "$1"; do
$cmd "$i" &>/dev/null
done
else
$cmd "$LGT_DESC" &>/dev/null
fi
}
7条答案
按热度按时间55ooxyrt1#
名为
errorlevel
的伪环境变量存储退出代码:此外,
if
命令具有特殊语法:有关详细信息,请参见
if /?
。示例
errorlevel
,%errorlevel%
将返回该值,而不是退出代码。使用(set errorlevel=
)清除环境变量,允许通过%errorlevel%
环境变量访问errorlevel
的真实值。*whitzsjs2#
测试
ErrorLevel
适用于 console 应用程序,但正如by dmihailescu所暗示的,如果您试图运行一个 windowed 应用程序,则此测试将不起作用(例如Win32架构)。 windows 化的应用程序会在背景执行,并且控制将立即返回到命令提示符(很可能ErrorLevel
为零,表示进程已成功 * 创建 *)。当窗口化应用程序最终退出时,其退出状态将丢失。不过,与使用其他地方提到的基于控制台的C++启动器不同,一个更简单的替代方法是使用命令提示符的
START /WAIT
命令启动窗口应用程序。这将启动窗口应用程序,等待它退出,然后将控制返回给命令提示符,并在ErrorLevel
中设置进程的退出状态。mcvgt66p3#
使用内置ERRORLEVEL变量:
可是beware if an application has defined an environment variable named ERRORLEVEL!
gkn4icbw4#
如果您想完全匹配错误代码(例如等于0),请使用以下代码:
注意
if errorlevel 0
与errorlevel
〉= 0匹配。请参阅
if /?
。或者,如果你不成功:
yruzcnhs5#
值得注意的是,.BAT和.CMD文件的操作方式不同。
阅读https://ss64.com/nt/errorlevel.html时,会注意到以下内容:
.CMD和.BAT批处理文件设置错误级别的方式有一个关键区别:
运行“新”内部命令的旧.BAT批处理脚本:APPEND、ASSOC、PATH、PROMPT、FTYPE和SET只在发生错误时设置ERRORLEVEL。因此,如果批处理脚本中有两个命令,并且第一个命令失败,即使第二个命令成功,ERRORLEVEL仍将保持设置。
这会使调试问题BAT脚本更加困难,CMD批处理脚本更加一致,并且会在您运行[source]的每个命令后设置ERRORLEVEL。
当我执行连续的命令时,这给我带来了无尽的悲伤,但即使在失败的情况下,ERRORLEVEL也不会改变。
zf9nrax16#
当使用未连接到控制台的程序时,它可能无法正常工作,因为当您认为您有退出代码时,该应用程序可能仍在运行。在C++中实现此目的的解决方案如下所示:
oxosxuxt7#
有一次我需要准确地将Cygwin的日志事件推送到Windows事件日志中。我希望WEVL中的消息是自定义的,有正确的退出代码、详细信息、优先级、消息等。所以我创建了一个小Bash脚本来处理这个问题。它在GitHub上,logit.sh。
部分摘录:
下面是临时文件内容部分:
以下是在WEVL中创建事件的函数:
执行批处理脚本并在__create_event上调用: