shell脚本重定向输出

发布于 2024-12-14 19:52:48 字数 587 浏览 0 评论 0原文

我正在寻找有关 shell 脚本的帮助,以将命令的输出重定向到文件。我有一个 C 程序,可以从串行端口读取输入并显示。我希望这些数据被重定向到一个文件。我正在通过调用从 java 程序执行此操作

Runtime r = Runtime.getRuntime();
Process procObj = r.exec("sh " + scriptfile);

我已尝试编写脚本文件,因为

./program >> file.txt

file.txt 未更新。在这里,程序直到与端口的连接丢失才结束,从某种意义上说,它是无限运行的。因此,我的程序不断寻找端口上的数据,并在数据存在时进行显示。 我只需要将相同的输出重定向到我将用作日志的文件。 我查看了 如何进行 shell 输出重定向(>)在脚本仍在运行时写入?但没有帮助。

请帮忙..

I am looking for a help regarding a shell script to redirect the output of a command to a file. I have a C program that reads the input from a serial port and display. I want this data to be redirected to a file. I am executing this from a java program by calling

Runtime r = Runtime.getRuntime();
Process procObj = r.exec("sh " + scriptfile);

I have tried writing the script file as

./program >> file.txt

The file.txt is not getting updated. Here, the program doesn't end until the connection to the port is lost, in a sense it is infinitely running. So my program keeps looking for data on the port and display as and when it is there.
I just need to redirect the same output to a file that I would use as a log.
I looked at How to make shell output redirect (>) write while script is still running? but not helpful.

Kindly help..

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

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

发布评论

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

评论(4

梨涡 2024-12-21 19:52:48

program 生成多少输出?使用标准 IO 重定向将在 stdoutfile 之间添加 4KB 缓冲区。这意味着在操作系统开始写入文件之前,您的程序必须输出超过 4KB 的数据。

要解决此问题,请在“工作单元”完成时(可能是一行,但也可能不止一行)将 stdout.flush() 添加到您的程序中。

How much output does program generate? Using standard IO redirection will add a 4KB buffer between stdout and file. This means your program must output more than 4KB of data before the OS starts to write to the file.

To fix this, add stdout.flush() to your program when a "work unit" is complete (maybe a line but might be more than one line).

江湖彼岸 2024-12-21 19:52:48

你可以尝试 ./program >> file.txt 2>>file.txt,还是./program 2>&1>>file.txt

Can you try ./program >> file.txt 2>>file.txt, or ./program 2>&1 >>file.txt?

硬不硬你别怂 2024-12-21 19:52:48

试试这个

    List<String> cmd = new ArrayList<String>();
    cmd.add("sh");
    cmd.add("-c");
    cmd.add("program 1> file.txt 2>&1");

    ProcessBuilder pb = new ProcessBuilder(cmd);
    Process p = pb.start();

just try this

    List<String> cmd = new ArrayList<String>();
    cmd.add("sh");
    cmd.add("-c");
    cmd.add("program 1> file.txt 2>&1");

    ProcessBuilder pb = new ProcessBuilder(cmd);
    Process p = pb.start();
梦罢 2024-12-21 19:52:48

如果您使用标准 C 调用进行输出(printfputs 等),您的输出可能会被缓冲。在 C89 及更高版本上,它取决于缓冲模式(无缓冲、完全缓冲、行缓冲)以及缓冲区的大小、输出是否完全缓冲以及缓冲区何时刷新(请参阅 http://www.gnu.org/software/libc/manual/html_node/Buffering-Concepts.htmlman setvbuf)。

默认情况下,Linux 上的文件输出是完全缓冲的。如果您希望输出立即出现在输出文件中,您可以:

此行为与您启动 C 程序这一事实无关通过 shell 脚本在 Java 程序中。此行为取决于您链接到程序中的标准 C 库。

If you use standard C calls for output (printf, puts etc.), your output may get buffered. On C89 and onwards, it depends on the buffering mode (unbuffered, fully buffered, line buffered) and on the size of the buffer, whether your output is buffered at all and when the buffer is flushed (see http://www.gnu.org/software/libc/manual/html_node/Buffering-Concepts.html and man setvbuf).

By default, output to a file is fully buffered on Linux. If you want the output to appear immediately in the output file, you may:

This behaviour is not related on the fact the you start your C program in a Java program via a shell script. This behaviour depends on the standard C library that you have linked into your program.

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