shell脚本重定向输出
我正在寻找有关 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
program
生成多少输出?使用标准 IO 重定向将在stdout
和file
之间添加 4KB 缓冲区。这意味着在操作系统开始写入文件之前,您的程序必须输出超过 4KB 的数据。要解决此问题,请在“工作单元”完成时(可能是一行,但也可能不止一行)将
stdout.flush()
添加到您的程序中。How much output does
program
generate? Using standard IO redirection will add a 4KB buffer betweenstdout
andfile
. 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).你可以尝试
./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
?试试这个
just try this
如果您使用标准 C 调用进行输出(
printf
、puts
等),您的输出可能会被缓冲。在 C89 及更高版本上,它取决于缓冲模式(无缓冲、完全缓冲、行缓冲)以及缓冲区的大小、输出是否完全缓冲以及缓冲区何时刷新(请参阅 http://www.gnu.org/software/libc/manual/html_node/Buffering-Concepts.html 和man setvbuf
)。默认情况下,Linux 上的文件输出是完全缓冲的。如果您希望输出立即出现在输出文件中,您可以:
fflush()
write()
(man 2 写入
)setvbuf(stdout, NULL, _IONBF, 0);
(https://stackoverflow.com/a/7876756/601203)此行为与您启动 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 andman 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:
fflush()
after each output operationwrite()
(man 2 write
)setvbuf(stdout, NULL, _IONBF, 0);
(https://stackoverflow.com/a/7876756/601203)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.