2

生の入力を受け取るPythonファイルを実行するシェルスクリプトを作成しようとしています。Pythonスクリプトは、基本的に次の行に沿っています。

def main():
    name=raw_input("What's your name?")
    print "Hello, "+name
main()

シェルスクリプトでスクリプトを実行し、入力を自動的にフィードするようにします。Python関数が返すものからシェル入力を取得する方法や、入力を使用してPythonからシェルを実行する方法をたくさん見てきましたが、この方法ではありません。基本的に、私は次のようなことをしたいだけです。

python hello.py
#  give the python script some input here
#  then continue on with the shell script.
4

2 に答える 2

8

次のような意味ですか?

python hello.py <<EOF
Matt
EOF

これは、bashヒアドキュメントとして知られています。

于 2013-02-19T19:01:05.283 に答える
4

生の入力を与えるより良い方法は本当にありませんsys.stdin。クロスプラットフォームでもあります。

import sys
print "Hello {0}!".format(sys.stdin.read())

それで

echo "John" | python hello.py # From the shell
python hello.py < john.txt # From a file, maybe containing "John"
于 2013-02-19T19:08:41.180 に答える