No output from awk only when redirected to pipe or file [duplicate]

半腔热情 提交于 2020-01-11 11:50:30

问题


I have a rather simple script (print content from a tty after adding timestamp to every row). It outputs nicely on the command line, but redirecting the output with > does not work. Why not?

Here is the script:

#!/bin/bash
awk '{ print strftime("%Y-%m-%d %H:%M:%S |"), $0; }' "$1"

Running it as is, like timecat /dev/ttyACM0 works fine, I see the content in my terminal.

But if I run timecat /dev/ttyACM0 > ~/tmp.log, nothing comes out. Same with tee. The file is there, but it is empty.

Is there something weird with awk in the script, how can I modify this to make the redirection work?


回答1:


All this needed was to flush the print within the command, replacing the last script row with this:

awk '{ print strftime("%Y-%m-%d %H:%M:%S |"), $0; fflush(); }' $tty
                                                  ^^^^^^^^^


来源:https://stackoverflow.com/questions/54450368/no-output-from-awk-only-when-redirected-to-pipe-or-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!