首页 > 解决方案 > 在 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)

如果您给我建议,我将不胜感激

标签: pythonbash

解决方案


让我们简化一下...

像这样创建 test.py:

print('Hello world')

然后,在您的 Bash shell 中:

OUTPUT=$(python test.py)
echo $OUTPUT

推荐阅读