我有一个 python 脚本,我想以这种方式从 bash 脚本运行:
#!/bin/bash
python -c "$(< input_file)" &> output_file
在 python 脚本中我有一些不同的方法,因此输入文件包含如下内容:
from script import *; method_1(); method_2();
问题是,在这两种方法中,它们都有一个需要用户输入的 input()
方法(无法更改)。
那么我如何在 input_file
中传递一个参数(某种换行参数),以便它被传递给 中的
还是 input()
方法method_1()method_2()
?
最佳答案
一个方便的方法是使用“这里文档”:
$ cat myscript
#!/bin/bash
python -c "$(< input_file)" &> output_file << END
3
4
END
这是一个独立的测试用例:
$ cat input_file
height = input("Height:\n")
width = input("Width:\n")
print "Area: ", height*width
$ bash myscript
(no output)
$ cat output_file
Height:
Width:
Area: 12
关于Python命令解释器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28485463/