Path: utzoo!mnetor!uunet!husc6!hao!gatech!mcnc!uvaarpa!virginia!scl From: scl@virginia.acc.virginia.edu (Steve Losen) Newsgroups: comp.unix.questions Subject: Re: Detecting pipe closure in shell script Message-ID: <553@virginia.acc.virginia.edu> Date: 18 Jan 88 15:12:38 GMT References: <380@dlhpedg.co.uk> Reply-To: scl@virginia.edu Organization: University of Va., Charlottesville, VA Lines: 33 In article <380@dlhpedg.co.uk> cl@datlog.co.uk (Charles Lambert) writes: >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 >scrap $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. -- Steve Losen scl@virginia.edu University of Virginia Academic Computing Center