[英]Python command interpreter
我有一個python腳本,我想以這種方式從bash腳本運行:
#!/bin/bash
python -c "$(< input_file)" &> output_file
在python腳本中,我有一些不同的方法,因此輸入文件包含以下內容:
from script import *; method_1(); method_2();
問題在於,在這兩種方法中,它們都有一個input()
方法,需要用戶輸入(無法更改)。
那么,如何在input_file
傳遞參數(某種換行參數),以便將其傳遞給method_1()
或method_2()
的input()
方法?
一種方便的方法是使用“ here文檔”:
$ 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
我相信input
功能只是從標准輸入中讀取。
因此,您應該能夠將數據傳遞(或重定向)到該python調用,以獲取input
,我想。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.