Inputs and outputs in the right order

Matthew Seaman matthew at FreeBSD.org
Fri Nov 1 06:48:24 UTC 2013


On 01/11/2013 02:56, Martin McCormick wrote:
> 	One of the beauties of unix is the ability to open a
> shell such as omshell or even the Bourne or C shells and feed
> commands to the shell from a script. One can capture the output
> of the shell to generate log entries or status messages for the
> benefit of the operator of the application.
> 
> 	If you do that, however, there is an interesting
> problem. If one was to manually enter commands to the shell, the
> output looks like:
> 
> some_command
> shell_output for one or more lines and then
> some_other_command
> shell_response.
> 
> 	When an automated script is entering commands, what
> appears to happen is that standard input sucks in gobs of lines
> as fast as it can and then the shell runs them sequentially if
> possible so it all happens very fast on a modern system.
> 
> 	The problem is that if one is capturing the session, the
> shell usually doesn't echo each command as executed. If you use
> a special application such as script, one sees all the commands
> fed in as a block followed by the results of the sequential
> execution of the block of commands coming out as a block. It is
> sometimes hard to tell exactly what happened to make the job
> fail because one does not have the command string followed by
> the results.
> 
> 	I know that expect does a great job of logging a session
> but I am wondering if there is an easier way to get the shell
> input echoed interwoven with all the output. In shorter words,
> what is the easiest way to produce a session log that reads like
> what actually happened when it happened?
> 
> Thanks for any and all constructive ideas.

When a process -- any process, which includes shells -- is reading from
a tty, then the input is set to line bufferred.  The reading process see
a chunk of input as soon as you hit return.

However, when the input comes from a pipe or just by reading a file,
then the input is set to block buffered, so a chunk of text 4kB in size
is read each time.

Now, with most programming languages you can control the buffering of
their input (eg. perl has the '$|' variable).  As far as I can see,
there isn't a way to do that from *within* a shell script, but you can
force the shell to behave interactively by invoking it as '/bin/sh -i'

	Cheers,

	Matthew

-- 
Dr Matthew J Seaman MA, D.Phil.
PGP: http://www.infracaninophile.co.uk/pgpkey


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 1036 bytes
Desc: OpenPGP digital signature
URL: <http://lists.freebsd.org/pipermail/freebsd-questions/attachments/20131101/df2232fc/attachment.sig>


More information about the freebsd-questions mailing list