如何在使用T恤时输入stdin

时间:2016-08-07 05:47:36

标签: bash tee

我通常使用tee将程序保存到其他文件中,例如

python -c "print('haha');" | tee -a /tmp/tmp.txt

但是,当程序需要从stdin读取

时,这不起作用
python -c "print('haha');import pdb; pdb.set_trace()" | tee -a /tmp/tmp.txt

程序将挂在那里而不打印任何东西。怎么处理这个?感谢...

2 个答案:

答案 0 :(得分:1)

尝试:

python -c "print('haha');import pdb, sys; pdb.Pdb(stdout=sys.stderr).set_trace()" | tee -a /tmp/tmp.txt

或者,如果你在Linux上:

stdbuf -o0 python -c "print('haha');import pdb, sys; pdb.set_trace()" | tee -a /tmp/tmp.txt

在上面的第一种情况中,pdb被指示将其输出发送到stderr。这意味着它不会转到tee,而是直接转到终端。

在第二种情况下,stdbuf实用程序用于关闭输出缓冲。 stdbuf是GNU coreutils的一部分。

改进版

当上面的代码运行时,pdb的stdout不是tty时表现不同。特别是,它不支持所有pdb的交互式命令行功能,例如控制键。正如blueyed指出的那样,script command能够克服这个问题,以便程序运行就像它的stdout没有被重定向一样:

script -q -c 'python -c "import pdb, sys; pdb.set_trace()"' /dev/null | tee -a /tmp/tmp.txt

答案 1 :(得分:0)

我不是python专家,但你需要从管道程序中刷新输出,否则你'哈哈'会卡在输出缓冲区中。

相关问题