【发布时间】:2011-06-02 18:43:51
【问题描述】:
我想让 Python 脚本从 shell (bash) 读取标准输入,并将标准输出和重定向文件发送到 shell。我尝试了以下方法:
$ cat test.py
#!/usr/bin/python
val = raw_input("enter val: ")
print val
$ ./test.py | tee out
testing
enter val: testing
$ cat out
enter val: testing
由于某种原因,在我输入输入后会打印 raw_input 提示,这意味着我在输入时看不到提示。使用 bash 脚本,我可以得到类似的工作。
$ cat test.sh
#!/bin/bash
echo "enter val: "
read val
echo $val
$ ./test.sh | tee out
enter val: testing
testing
$ cat out
enter val: testing
【问题讨论】: