title: Write file instantly (without line buffering) in shell script
body: <p>I have a script like this,</p>
  
  <pre><code>#!/bin/bash
  
  echo "hello" &gt; a.txt
  
  while [ True ]; do
  
  #do something here, very long and possiblely causing system reboot
  
  done
  </code></pre>
  
  <p>It looks like I can only get a normal a.txt (with "hello" inside) if the execution is ended normally. In the case that the "while loop" is running or a system reboot was triggered during the while loop, there will be a blank a.txt.</p>
  
  <p>By Google searching, it seemed to be a line-bufferring issue as shell turns on line bufferring by default. As there is no "Expect" or "stdbuf" installed on my customized Ubuntu, I tried to replace</p>
  
  <pre><code>echo "hello" &gt; a.txt
  </code></pre>
  
  <p>with a Perl/C program, where line buffer is disabled. In Perl, I used "$|" and "fflush" was used in C. Unfortunately, it doesn't work.</p>
  
  <p>Any feedback is appreciated...</p>