Python命令解释器

标签 python linux bash command-line interpreter

我有一个 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/

相关文章:

ruby-on-rails - 'bin'中的 '$ bin/rails?'是什么意思

linux - 谷歌计算引擎 DEBIAN :/etc/group setting lost after Reboot

c - 我想要 linux 上 C 中的一个函数来收集核心转储而不终止进程

bash - 使用 awk 重新格式化文本文件并将其剪切为单行

python - 当数字从两位数变为一位数时,如何使屏幕上的字体居中?

python - 如何设计一个数据库来表示结构变化的数据?

python - FastAPI和Python请求422错误问题

python - Matplotlib - 固定 x 轴刻度和自动缩放 y 轴

linux - 在 centos 中只为单个用户安装没有 sudo 的软件?

bash - 为什么这个 sed 命令不匹配空格?