python - 在 bash 脚本中从 python 访问变量
问题描述
我想在 bash 脚本中从 python 访问打印值
例如python代码
# test.py
def main():
print('True')
if __name__ == '__main__':
s = input()
if s == "True":
main()
而且,还有示例(ex.sh)bash 脚本
#!/bin/bash
echo {EX} | python test.py
命令:
bash ex.sh True
我希望在 bash 中从 python 加载变量。这是错误的,但其中一个想法是:
OUTPUT=$(echo {EX} | python test.py 2>&1)
如果您给我建议,我将不胜感激
解决方案
让我们简化一下...
像这样创建 test.py:
print('Hello world')
然后,在您的 Bash shell 中:
OUTPUT=$(python test.py)
echo $OUTPUT
推荐阅读
- python - 如何从 Python 中的文件中读取与起始模式匹配的一大块行?
- r - 将 3d 图形保存为多种格式
- r - 重新排序大数据框中的列
- python - 热以避免 Python Dask Logistic 回归 Multiple constant columns detected 错误
- gradle - 如何将`intellij gradle plugin`添加到我的自定义插件依赖项中
- tcl - TCL:将单个变量解释为多个参数
- php - 使用 php 检索 youtube 频道的总喜欢和不喜欢
- python - 如何在 Keras 中实现 Matthews 相关系数?
- powershell - 使用从 Windows ID 中找到的数据并通过 Powershell 将其放入电子邮件中
- javascript - 使用javascript动态创建新元素时如何推送现有的DOM元素