[Wylug-help] Bash Scripting question.

Frank Shute Frank Shute <frank at esperance-linux.co.uk>
Wed, 26 Feb 2003 14:30:57 +0000


On Wed, Feb 26, 2003 at 03:28:31AM -0700, Thomas, Nicholas wrote:
>
>
> Hi,
>
> I have a bash script to write and it would be nice if it had the following
> attributes:
>
> 1) A simple script which throws out text messages to the console when
> running in the foreground, using the echo command.
> Whilst simultaneously sending the same output to a log file.

Well, tee is the utility you need for this eg:

$ echo "foo" | tee -a ~/logfile

>From my manpage for tee:

DESCRIPTION
     The tee utility copies standard input to standard output, making a copy
     in zero or more files.  The output is unbuffered.

>
> 2) The script can be sent to the background (Using Ctrl-Z then 'bg' ). The
> log file still gets its message text, but the console no longer
> gets any messages. The script continues to run in the background.
>
> 3) I can resume foreground operation, using 'fg', and the console messages
> start coming out on the console again. I am not worried about losing
> the console messages which were generated while the script was in the
> background because I can review the log file for the complete list.
>
> I presume this is possible?

It sounds like you have to find a way to program your script so that
if running in the foreground send stout to /dev/console but if not
don't.

The only way I can think of is running your script in the background
with stout going to your logfile but on receipt of a signal send stout
to /dev/console.

I've never written a script to catch a signal but it can be done.

But I'd agree with Jim, the *elegant* way is just to tail -f your
logfile rather than get involved in ugly signal hackery!

--

 Frank

*-*-*-*-*-*-*-*-*-*-*
   Boroughbridge.
 Tel: 01423 323019
     ---------
PGP keyID: 0xC0B341A3
*-*-*-*-*-*-*-*-*-*-*

http://www.esperance-linux.co.uk/

Droid:  Actually, Microsoft does produce a quality product.

Cynic:  True, but you can't run a server or a decent desktop with just
        an intellimouse...

	- seen on Slashdot