当我做类似的事情时:
$ echo "print \"test\"" | python
我想:
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>> print "test"
test
代替:
test
有没有办法捕获标准输出中的所有提示?
答案1
您可以将评论中提出的所有建议与这样的这里的字符串混合在:
$ script -c 'python -i <<< "print \"test\""'
Script started, file is typescript
Python 2.7.5 (default, Jul 13 2018, 13:06:57)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> test
>>>
Script done, file is typescript
这会将以上内容记录到一个名为的文件中typescript
:
$ cat typescript
Script started on Tue 21 Aug 2018 12:19:50 AM EDT
Python 2.7.5 (default, Jul 13 2018, 13:06:57)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> test
>>>
Script done on Tue 21 Aug 2018 12:19:50 AM EDT
上面的方法的工作原理如下:
script -c
'...'- runs the commands in single quotes in
脚本and logs the output to the file
打字稿`python -i <<< "...."
- 以交互模式运行 Python,传递命令,"...."
作为这里的字符串"print \"test\""
- 作为命令运行这里的字符串
带有转义序列的打字稿
如果您发现生成的typescript
文件包含转义序列,例如:
ESC[34mRPMsESC[39;49mESC[0m
ESC[34mRPMs_fpmESC[39;49mESC[0m
ESC[34mansibleESC[39;49mESC[0m
您可以使用less -R
或less -r
查看这些:
$ less -R somefile
RPMs
RPMs_fpm
ansible