在日志文件中搜索 - 退出代码
Search in logfile - exit code
我有一些遗留软件需要在 Control-M 下自动化。这些作业在 Windows 2008R2.
下
如果这些作业 运行 正常,但如果它们可以处理一些错误,则它们的退出代码为 0。当日志中有特定字符串时,我需要发出警报。
该字符串不在可执行文件的输出中。
我为此实施了另一项工作。它会在文件中搜索一个字符串,在 "On Do Actions" 我搜索语句。
要在输出中包含语句,我想使用类似 grep 的东西。我用过:
findstr
findstr "myerrorcode" D:\Log\greptest_%%$ODATE..log
grep
在 cygwin
下
在这两种情况下,我都有相同的情况:
- 找到字符串就ok了
- 如果文件未找到或无法打开,grep 或 findstr return 退出代码 = 1。这没关系,因为作业必须引发错误。
但问题是:当在文件中找不到字符串时,grep 和 findstr 都有一个 return code = 1.
如何区分文件无法打开和运行一切正常,但未找到日志中的 sring 的情况?
您应该能够使用 grep
的退出状态来检测失败的原因。根据 POSIX grep
docs,退出状态部分:
EXIT STATUS
The following exit values shall be returned:
0 One or more lines were selected.
1 No lines were selected.
>1 An error occurred.
GNU grep
类似(一致,但更具体):
Normally the exit status is 0 if a line is selected, 1 if no lines were selected, and 2 if an error occurred. [...] Other grep implementations may exit with status greater than 2 on error.
例如,在 bash
中,您可以使用 case
命令来处理多个分支,如下所示:
#!/bin/bash
# search for error code in file
grep code file
# store the exit status in variable err
err=$?
# test several cases
case $err in
0) echo All good.;;
1) echo Code not found.;;
*) echo Error reading from file.;;
esac
您可以在 Control-M 中轻松处理:
在作业选项卡中添加 "Action"
- 添加"On Do Action"
- 开启:"Specific statement output"
- 声明(作为 Control-M 文档状态):
A character string, from 1 through 132 characters in length, containing a statement from the job script file The specified string can be a portion of the statement.
Statement character strings can each contain mask characters. Valid
mask characters are:
* – represents any number of characters (including no characters)
$ – represents any single character
? – represents any single character
- 代码:
A character string, from 1 through 255 characters in length, to be
compared to the operating system’s response to the specified
statement.
Code character strings can each contain mask characters. Valid mask
characters are:
* – represents any number of characters (including no characters)
$ – represents any single character
? – represents any single character
示例:
On Do Action Control-M 8
我有一些遗留软件需要在 Control-M 下自动化。这些作业在 Windows 2008R2.
下如果这些作业 运行 正常,但如果它们可以处理一些错误,则它们的退出代码为 0。当日志中有特定字符串时,我需要发出警报。
该字符串不在可执行文件的输出中。
我为此实施了另一项工作。它会在文件中搜索一个字符串,在 "On Do Actions" 我搜索语句。
要在输出中包含语句,我想使用类似 grep 的东西。我用过:
findstr
findstr "myerrorcode" D:\Log\greptest_%%$ODATE..log
grep
在 cygwin 下
在这两种情况下,我都有相同的情况:
- 找到字符串就ok了
- 如果文件未找到或无法打开,grep 或 findstr return 退出代码 = 1。这没关系,因为作业必须引发错误。
但问题是:当在文件中找不到字符串时,grep 和 findstr 都有一个 return code = 1.
如何区分文件无法打开和运行一切正常,但未找到日志中的 sring 的情况?
您应该能够使用 grep
的退出状态来检测失败的原因。根据 POSIX grep
docs,退出状态部分:
EXIT STATUS The following exit values shall be returned: 0 One or more lines were selected. 1 No lines were selected. >1 An error occurred.
GNU grep
类似(一致,但更具体):
Normally the exit status is 0 if a line is selected, 1 if no lines were selected, and 2 if an error occurred. [...] Other grep implementations may exit with status greater than 2 on error.
例如,在 bash
中,您可以使用 case
命令来处理多个分支,如下所示:
#!/bin/bash
# search for error code in file
grep code file
# store the exit status in variable err
err=$?
# test several cases
case $err in
0) echo All good.;;
1) echo Code not found.;;
*) echo Error reading from file.;;
esac
您可以在 Control-M 中轻松处理: 在作业选项卡中添加 "Action"
- 添加"On Do Action"
- 开启:"Specific statement output"
- 声明(作为 Control-M 文档状态):
A character string, from 1 through 132 characters in length, containing a statement from the job script file The specified string can be a portion of the statement.
Statement character strings can each contain mask characters. Valid mask characters are:
* – represents any number of characters (including no characters) $ – represents any single character ? – represents any single character
- 代码:
A character string, from 1 through 255 characters in length, to be compared to the operating system’s response to the specified statement.
Code character strings can each contain mask characters. Valid mask characters are:
* – represents any number of characters (including no characters) $ – represents any single character ? – represents any single character
示例: On Do Action Control-M 8