When is the output of a script added to a file and what can I do to bypass the result?

advertisements

I have a script called my_bash.sh and it calls a perl script, and appends the output of the perl script to a log file. Does the file only get written once the perl script has completed?

my_bash.sh

#!/bin/bash
echo "Starting!" > "my_log.log"
perl my_perl.pl >> "my_log.log" 2>&1
echo "Ending!" >> "my_log.log"

The issue is that as the perl script is running, I'd like to manipulate contents of the my_log.log file while it's running, but it appears the file is blank. Is there a proper way to do this? Please let me know if you'd like more information.

my_perl.pl

...
foreach $component (@arrayOfComponents)
{
  print "Component Name: $component (MORE_INFO)\n";

  # Do some work to gather more info (including other prints)
  #...

  # I want to replace "MORE_INFO" above with what I've calculated here
  system("sed 's/MORE_INFO/$moreInfo/' my_log.log");
}

The sed isn't working correctly since the print statements haven't yet made it to the my_log.log.


Perl would buffer the output by default. To disable buffering set $| to a non-zero value. Add

$|++;

at the top of your perl script.

Quoting perldoc pervar:

$|

If set to nonzero, forces a flush right away and after every write or print on the currently selected output channel. Default is 0 (regardless of whether the channel is really buffered by the system or not; $| tells you only whether you've asked Perl explicitly to flush after each write). STDOUT will typically be line buffered if output is to the terminal and block buffered otherwise. Setting this variable is useful primarily when you are outputting to a pipe or socket, such as when you are running a Perl program under rsh and want to see the output as it's happening. This has no effect on input buffering. See getc for that. See select on how to select the output channel. See also IO::Handle.

Mnemonic: when you want your pipes to be piping hot.