如何将进程的输入文本和输出文本捕获到文件中,就像从 shell 运行时出现的那样

发布于 2024-12-08 21:05:10 字数 1048 浏览 0 评论 0原文

我需要通过运行某些程序并验证输出来测试它们,并将整个测试数据复制到报告中。通过整个测试数据,我的意思是程序的输出(stdout)和程序的输入(stdin)都需要在报告中。例如,假设我运行 python 来做一些计算。一个简单的会话可能如下所示:

Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> math.sqrt(2)
1.4142135623730951
>>> math.log(2)
0.6931471805599453
>>> 2 ** 32
4294967296
>>> 

在这里,我有输入和输出(计算的提示和结果)。

我的问题是,给定一个程序,并且可能包含一个包含该程序的标准输入的文件,我怎样才能获取该程序运行的所有文本数据,就像我以交互方式运行它一样,就像上面的 python 会话一样?换句话说,给定 python 程序和文本文件(用于输入),我如何从 python 会话中获取上述文本,其中包含:

import math
math.sqrt(2)
math.log(2)
2 ** 32

我可以在 shell 中运行该程序并复制输出,但它看起来不像干净、系统的方法。相反,我正在考虑编写另一个程序,该程序将给定的程序作为参数以及文件名(从中读取输入)。第二个程序将从给定文件中读取,分叉给定程序,将文本从文件转发到给定程序(通过管道),并读取给定程序的输出(也通过管道)。这样,这个中间程序就可以读取输入和输出,并将它们写入另一个文件。

我预见的唯一问题是,由于流缓冲区等原因,最终输出文件中输入和输出的放置将被关闭,并且我将无法获得预期的输出。那么,我的逻辑是否正确?如果是,我能否按照我想要的方式获得输出?是否有更简单的方法来执行此操作,例如,已经存在执行此操作的程序?

抱歉这么长的解释。感谢您的回复。

I need to test some programs by running them and verifying the output, and the entire test data shall be copied into a report. By entire test data, I mean both the output (stdout) of the program and the input (stdin) to the program need to be in the report. For example, let's say I run python to do some calculations. A simple session may look like this:

Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> math.sqrt(2)
1.4142135623730951
>>> math.log(2)
0.6931471805599453
>>> 2 ** 32
4294967296
>>> 

Here, I have my inputs and the outputs (the prompts and results of my calculations).

My question is, given a program and possibly a file containing the stdin for that program, how can I get all textual data of the program's run as though I ran it interactively, like the above python session? In other words, how can I get the above text from the python session given the python program and a text file (for input) containing:

import math
math.sqrt(2)
math.log(2)
2 ** 32

I could run the program in a shell and copy the output, but it doesn't seem like a clean, systematic way of doing it. Instead, I'm thinking writing another program that takes the given program as an argument as well as a filename (from which to read input). This second program would read from the given file, fork the given program, forward the text from the file to the given program (by means of a pipe), and read the output of the given program (also by a pipe). That way this intermediary program can read both the input and the output, and can write them to another file.

The only problem I foresee is that due to stream buffers and what not, the placement of the inputs and outputs in the final output file will be off, and I won't get the intended output. So, is my logic correct, and if so, will I be able to get the output the way I want it? Is there a simpler way to do this, e.g., a program already exists that does this?

Sorry for the long explanation. Thanks for your responses.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的黑色迷你裙 2024-12-15 21:05:10

尝试脚本命令。默认情况下,它仅捕获输出,但它也可以捕获输入。

script -akq capture.log my-fun-little-command and its args

Try the script command. By default it only captures output, but it can also capture input as well.

script -akq capture.log my-fun-little-command and its args
日久见人心 2024-12-15 21:05:10

您可以将管道用于基于文件的版本:

cat commands.txt|python > results.txt

否则,请查看类似 expect 的内容来控制另一个程序,如 python shell。

You can use pipes for the file-based version:

cat commands.txt|python > results.txt

Otherwise, look into something like expect to control another program, like the python shell.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文