Detecting pipe closure in shell script

Detecting pipe closure in shell script

Post by Charles Lambe » Tue, 02 Jan 1990 17:47:00



I have a shell script with the following fragment in it:

    | tee $TFILE | more

If I quit more(1) before output from tee(1) is complete, I want to
s*$TFILE,  else I want to move it to another name.  I've tried

    | ( trap 'rm $TFILE' 13; tee ) | more;
    mv $TFILE othername

and

    | ( tee ; mv $TFILE othername) | more

but in both cases the move occurs regardless of completion (in the second
case, the "mv" seems to be executing even after shell script has exited).

Any smart tricks?

----------------------
Charles Lambert

 
 
 

Detecting pipe closure in shell script

Post by David Hi » Tue, 02 Jan 1990 11:40:00



>I have a shell script with the following fragment in it:

>    | tee $TFILE | more

>If I quit more(1) before output from tee(1) is complete, I want to
>s*$TFILE,  else I want to move it to another name.  I've tried
...
>Charles Lambert

Try

        | (tee "$TMP" && mv "$TMP" "$REAL" || rm "$TMP" ) | more

If the more dies, then the tee fails because it cannot finish its
writes.  Note that the code above also has the effect of removing
the $TMP file if the mv failes.
--
Dave Hitz


 
 
 

Detecting pipe closure in shell script

Post by Steve Los » Tue, 02 Jan 1990 16:12:00



>I have a shell script with the following fragment in it:

>    | tee $TFILE | more

>If I quit more(1) before output from tee(1) is complete, I want to
>s*$TFILE,  else I want to move it to another name.  I've tried

Try this in the bourne shell:

... |
if tee $TFILE; then
        mv $TFILE newname
else
        rm $TFILE
fi | more

If you quit out of more, tee gets a SIGPIPE and a non-zero exit status,
causing the "else" arm of the conditional to be executed.  Note that in
/bin/sh you can pipe into and out of if, while, for, and case statements.

If you don't mind destroying the file "newname" when you quit out of more,
use this simpler solution:

... | (tee newname || rm newname) | more

in /bin/sh "||" and "&&" have short-circuit evaluation, so you can
conditionally execute commands based on the exit status of a previous
command.

--

University of *ia Academic Computing Center

 
 
 

1. How to prevent closure of child apps from aborting shell script?

Hi-
    I have a bash shell script running under KDE
that starts up kview and netscape asynchronously
with commands like

    kview "$file" &
    netscape "$file" &

    Later, when the program is waiting for user input,
if the user closes the kview window or the netscape
window, the shell script terminates.

    Any ideas on how to keep it from terminating?

    Thanks in advance.

-Bob

2. The Elm Connection

3. Detecting remote socket closure

4. PPP slow, help me!

5. Detecting pipe mode in a shell

6. Promise Ultra 66 and Linux

7. detecting a piped input from csh script

8. reboot fixes eth0 but not ifup/network restart

9. a question about 'read' from pipe in shell script

10. shell script style question: pipe or redir to file

11. pipe output from c program to a korn shell script

12. piping to/from shell script

13. Piping output to a C-shell script from another command - HOW?