python - 在 Shell 中使用日志重定向时如何获取函数的重调代码?
问题描述
在我的 shell scripts1 中,它可以工作。:</p>
测试1.sh
python main.py
if [[ $? -ne 0 ]]
then
exit
fi
测试2.sh
python main.py 2>&1 | tee log.txt
if [[ $? -ne 0 ]]
then
exit
fi
脚本 2 失败。main
在 test2.sh
中如何获取 python-call 的返回码?</p>
解决方案
如果在一行中使用多个命令,则需要使用PIPESTATUS
来为每个命令获取正确的返回码。
例如:
Python代码:
import sys
def exit_with_code():
sys.exit(5)
exit_with_code()
外壳脚本:
python3 test.py 2>&1 | tee log.txt
echo "${PIPESTATUS[0]} ${PIPESTATUS[1]}"
输出:
5 0
表示脚本的返回码Python
为5,脚本的返回码tee
为0。
推荐阅读
- sql-server - 如果日在月中,则返回 1
- android - 导航组件退出动画未按预期工作
- laravel - Laravel 动态生成的字段验证
- pandas - Pandas - How can I Improve execution time of a function in a pandas dataframe?
- javascript - 如何通过道具将功能传递给父组件是功能组件的chid组件
- r - 在 ggplot2 中绘制混合效果输出的随机部分
- google-cloud-platform - 是否可以在 GCS 中的一组文件上运行数据流 DLP 去识别作业?
- angular - 如何在不绑定到 ViewModel 的情况下设置 ngx-datatable 的 columnMode/...?
- python-requests - Python请求无法识别来自服务器对高延迟请求的响应
- blazor - 导航到我网站之外的页面时,NavigationManager LocationChanged 事件未触发