我有一个bash脚本set -x.是否可以将此脚本的调试打印及其所有输出重定向到文件?理想情况下,我想做这样的事情:
#!/bin/bash
set -x
(some magic command here...) > /tmp/mylog
echo "test"
Run Code Online (Sandbox Code Playgroud)
得到了
+ echo test
test
Run Code Online (Sandbox Code Playgroud)
在/ tmp/mylog中输出,而不是在stdout中输出.
有没有一种巧妙的方法可以在 Python 脚本中注入失败?我想避免在源代码中添加以下内容:
failure_ABC = True
failure_XYZ = True
def inject_failure_ABC():
raise Exception('ha! a fake error')
def inject_failure_XYZ():
# delete some critical file
pass
# some real code
if failure_ABC:
inject_failure_ABC()
# some more real code
if failure_XYZ:
inject_failure_XYZ()
# even more real code
Run Code Online (Sandbox Code Playgroud)
编辑: 我有以下想法:插入“失败点”作为特制的评论。编写一个简单的解析器,该解析器将在 Python 解释器之前调用,并将生成带有实际故障代码的实际检测 Python 脚本。例如:
#!/usr/bin/parser_script_producing_actual_code_and_calls python
# some real code
# FAIL_123
if foo():
# FAIL_ABC
execute_some_real_code()
else:
# FAIL_XYZ
execute_some_other_real_code()
Run Code Online (Sandbox Code Playgroud)
以 开头的任何内容FAIL_都被脚本视为故障点,并根据配置文件启用/禁用故障。你怎么认为?