[英]Create a log in Bash Script
There's two shells scripts: A.sh and B.sh 有两个shell脚本:A.sh和B.sh
Another script C.sh will create a log containing: 另一个脚本C.sh将创建一个包含以下内容的日志:
Example:
<My hostname>
<My username>
A.sh
12:09:00 p.m "Initialize shell" OK
12:10:00 p.m. "Creating file" OK
12:11:00 p.m. "Creating backup" FAIL
B.sh
01:00:00 p.m "Initialize shell" OK
01:01:00 p.m. "Creating file" FAIL
01:02:00 p.m. "Creating backup" FAIL
The point is to obtain the success/failure status of each function and the time at which was executed and save it all to a log file. 关键是要获取每个功能的成功/失败状态以及执行时间,并将其全部保存到日志文件中。 How do I check for the status of each function in a shell?
如何检查外壳中每个功能的状态?
1Simplest way is flush output from scripts in independent files and then include this log files into common file, generated by C.sh. 1最简单的方法是从脚本将刷新输出到独立文件中,然后将此日志文件包含到C.sh生成的公共文件中。 You could use
tee
*nix function in A.sh, B.sh to populate log files and filter only records you need. 您可以在A.sh,B.sh中使用
tee
* nix函数来填充日志文件并仅过滤所需的记录。 Also, it is possible to fetch errors in scripts via set +o errexit
and set +o pipefail
, and then check status of function executions right after the function: 同样,可以通过
set +o errexit
和set +o pipefail
来获取脚本中的错误,然后在函数之后检查函数执行的状态:
set +o errexit
func.sh param1 param2
if [ $? -ne 0 ] ; then
echo "FAIL" | tee 1.txt
exit 1 # if you need interrupt execution
else
echo "OK" | tee 1.txt
fi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.