Force a shell script to fflush

后端 未结 3 1784
深忆病人
深忆病人 2020-12-17 18:08

I was wondering if it was possible to tell bash that all calls to echo or printf should be followed up by a subsequent call to fflush()

相关标签:
3条回答
  • 2020-12-17 18:52

    If comands use stdio and are connected to a terminal they'll be flushed per line. Otherwise you'll need to use something like stdbuf on commands in a pipe line http://www.pixelbeat.org/programming/stdio_buffering/

    tl;dr: instead of printf ... try to put to the script stdbuf -o0 printf .., or stdbuf -oL printf ...

    0 讨论(0)
  • 2020-12-17 18:57

    Maybe "stty raw" can help with some other tricks for end-of-lines handling. AFAIK "raw" mode turns off line based buffering, at least when used for serial port ("stty raw < /dev/ttyS0").

    0 讨论(0)
  • 2020-12-17 19:01

    If you force the file to be read, it seems to cause the buffer to flush. These work for me.

    Either read the data into a useless variable:

        x=$(<$logfile)
    

    Or do a UUOC:

        cat $logfile > /dev/null
    
    0 讨论(0)
提交回复
热议问题