python - 从分配中的多个命令中获取退出代码
问题描述
假设我有一个类似于以下内容的命令:
VAR=$(python SomeScript | tee /dev/null)
我想获得 python 脚本的退出代码,但不确定如何在同一命令中分配。
解决方案
如果您只有一个退出代码要返回,您可以提取并exit
使用它来制作整个命令替换的退出代码:
var=$(
python -c 'import sys; print("hi"); sys.exit(42)' | cat
exit "${PIPESTATUS[0]}"
)
ret=$?
echo "The output is $var and the exit code is $ret"
这导致:
The output is hi and the exit code is 42
如果您需要提取多个退出状态,则必须将它们写入文件或流的末尾,然后再将它们读回或提取它们。
推荐阅读
- python - 如何使用哈希进行密码检查?
- python - 将值存储到init,但在读取新文件时它会不断覆盖列表
- sql-server - SQL Server 2008 R2 : restrict server login from seeing all but 2 databases on server
- vue.js - 无法在 ag-grid Vue 中读取未定义的属性“setQuickFilter”
- linux-kernel - 在同一个等待队列上等待不同的条件还是为每个条件使用单独的等待队列?
- android - 构建包含依赖 AAR 的 Android 库 AAR
- java - 创建一个实现 View.OnClickListener 的动态 onClick()
- c# - 如何从 libtiff.net TIFFCP.exe 捕获 C# 中的所有异常
- outlook-api - Outlook 休息 api ErrorNonExistentMailbox
- git - git 服务器在哪里存储推送的文件以及在客户端克隆时从哪里获取?