击输出流写入到文件 [英] Bash output stream write to a file

查看:139
本文介绍了击输出流写入到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在bash中运行以下命令:

so i am running this on bash:

# somedevice -getevent

这是什么命令的功能是它只是不断运行,并且每次我的设备发送一个特定的数据,说它检测温度的变化,它输出这样的事情

What this command does is it just keeps running, and everytime my device sends a certain data, say it detects change in temperature, it outputs something like this

/dev/xyz: 123 4567 8910112238 20
/dev/xyz: 123 4567 8915712347 19
/dev/xyz: 123 4567 8916412345 22
/dev/xyz: 123 4567 8910312342 25
/dev/xyz: 123 4567 8910112361 18
/dev/xyz: 123 4567 8910112343 20

而这只是保持运行,只要有任何导致它输出的东西。因此,有没有结束执行。

And this just keeps running and as soon as it has any cause it outputs something. So there is no end to execution.

没有回声正常使用,但是当我试图用'>'运算符这似乎并没有写入文件。

No the echo is working perfectly, however when i am trying to use the '>' operator this doesn't seem to write to file.

因此​​,例如

#somedevice -getevent > my_record_file

这不能正常工作,my_record_file只得到在间隔写入数据,但是我想立即写入。

this doesn't work properly, my_record_file only gets data written to it in intervals, however i want to be written immediately.

任何想法?

推荐答案

输出被缓冲,因为C标准库的改变取决于是否不是stdout是终端设备的输出缓冲模式。如果它是一个终端设备(根据 isatty(3) ),然后stdout是的行缓冲的:它被刷新每一个换行符被写入的时间。如果它不是一个终端设备,那么它的全缓冲的:它只是每当数据一定数量(通常是一些4 KB到64 KB的量级)获取写入被刷新

The output is being buffered because the C standard library changes the output buffering mode depending on whether or not stdout is a terminal device. If it's a terminal device (according to isatty(3)), then stdout is line-buffered: it gets flushed every time a newline character gets written. If it's not a terminal device, then it's fully buffered: it only gets flushed whenever a certain amount of data (usually something on the order of 4 KB to 64 KB) gets written.

所以,当你重定向命令的输出使用shell的&GT文件; 重定向操作,它不再输出到终端,并缓冲输出。程序可以改变它的缓冲模式 setvbuf用来(3) 和朋友,但该方案有合作,做到这一点。许多程序的命令行选项,使他们行缓冲,例如的grep(1) - 行缓冲选项。看看你的命令有一个类似的选项。

So, when you redirect the command's output to a file using the shell's > redirection operator, it's no longer outputting to a terminal and it buffers its output. A program can change its buffering mode with setvbuf(3) and friends, but the program has to cooperate to do this. Many programs have command line options to make them line-buffered, e.g. grep(1)'s --line-buffered option. See if your command has a similar option.

如果你没有这样的选项,你可以尝试使用工具,如 无缓冲(1) 以无缓冲输出流,但它并不总是工作,而不是一个标准的工具,所以它并非始终可用。

If you don't have such an option, you can try using a tool such as unbuffer(1) to unbuffer the output stream, but it doesn't always work and isn't a standard utility, so it's not always available.

这篇关于击输出流写入到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆