我想编写一个bash
应该调用多个python
脚本的脚本。该范围内python
的脚本是这,我不允许改变一些打印消息。我想将计算的最终状态解析到我的 bash 脚本来决定下一步要做什么。我的计划是构建这样的python
文件:
import sys
print('this is just some print messages within the script')
print('this is just some print messages within the script')
sys.stderr.write('0 or 1 for error or sucessfull')
并stderr
在 bash 脚本中重定向(但仍将print
函数的输出保留在终端上)
errormessage="$(python pyscript.py command_for_redirecting_stderr_only)"
有人可以帮我只重定向stderr
吗?我发现的所有解决方案都不会保留print
函数的输出(大多数人设置stdout
为 null)。
并且:如果有人有一个更聪明(更稳定)的想法来交出计算结果,我们将不胜感激。
预期输出:
pyscript.py
import sys
print('this is just some print messages within the script')
print('this is just some print messages within the script')
sys.stderr.write('0 or 1 for error or sucessfull')
脚本文件
#!/bin/bash
LINE="+++++++++++++++++++++++++"
errormessage="$(python pyscript.py command_for_redirecting_stderr_only)"
echo $LINE
echo "Error variable is ${errormessage}"
我打电话时的输出bash bashscript.sh
:
this is just some print messages within the script
this is just some print messages within the script
+++++++++++++++++++++++++
Error variable is 0/1
您可以交换 stderr 和 stdout 并将 stderr 存储在您可以在脚本末尾回显的变量中。所以尝试这样的事情:
#!/bin/bash
line="+++++++++++++++++++++++++"
python pyscript.py 3>&2 2>&1 1>&3 | read errormessage
echo "$line"
echo "Error variable is ${errormessage}"
这应该像往常一样打印您的标准输出,并在最后打印标准错误。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句