Bourn Shell Scripts that Produce Multiple Files

Martin McCormick martin at dc.cis.okstate.edu
Tue Apr 5 13:28:31 PDT 2005


	I wrote a Bourn Shell script which has a while loop in it that
reads a file line by line.  The output of the script is supposed to go
to a file with a different name for each iteration of the loop.  This
scheme is obviously a rotten idea because all the new files end up
created, but quite empty.  If I take out the > $newfilename.txt
directive, I get the proper output at stdout so the only problem is
with changing the file name in the middle of the game several times.

	I think that either the output gets lost in buffers or I am
killing the association between the file descriptor that was first
opened and all the new files that get created each time the loop runs.
bash doesn't have anything like a fflush(); function,
does it?

	I even tried piping the output through cat as in |cat
>$newfilename.txt and even |tee $newfilename.txt which sends the
output to stdout and to any files you give to tee as arguments.

	The results are always the same.  When I used tee, I could
read proper output being sent through stdout, but those files were
still as blank as ever.

	The man page for bash doesn't even contain the word flush nor
does it discuss output buffering regarding redirection or "closing"
open files.

	I actually made the script work by splitting it in to two
shell scripts which caused the part producing the output to exit each
time.  That certainly caused output to go to the proper files, but I
think there should be a way to make it all happen from one script.

	Thank you for any suggestions.

Martin McCormick WB5AGZ  Stillwater, OK 
OSU Information Technology Division Network Operations Group


More information about the freebsd-questions mailing list