[comp.unix.shell] How to interrupt shell functions?

eric@brolga.cc.uq.oz.au (Eric Halil) (06/21/91)

I have the following function (for bash1.08.1)

zless() {
for i in $* ; do
	zcat $i | less
done
}

What I'd like to be able to do is interrupt the function.  When I interrupt I
can only get (not surprisingly) the "zcat | less".  And the for loop continues
on it's merry way.  I thought putting a "trap 'return' 2" as the first line 
might work, but it didn't.  Any suggestions?

Thanks,
Eric.
--
Eric Halil                           |Internet/CSnet:         eric@cc.uq.oz.au
Prentice Computer Centre             |Bitnet:    eric%cc.uq.oz.au@uunet.uu.net
University of Queensland    4072     |UUCP:        uunet!munnari!cc.uq.oz!eric
AUSTRALIA --   Phone: +61 7 365 3610 |JANET:        eric%cc.uq.oz.au@uk.ac.ukc

chet@thor.INS.CWRU.Edu (Chet Ramey) (06/23/91)

In article <1991Jun21.002944.11023@brolga.cc.uq.oz.au> eric@brolga.cc.uq.oz.au (Eric Halil) writes:
>I have the following function (for bash1.08.1)
>
>zless() {
>for i in $* ; do
>	zcat $i | less
>done
>}
>
>What I'd like to be able to do is interrupt the function.  When I interrupt I
>can only get (not surprisingly) the "zcat | less".  And the for loop continues
>on it's merry way.  I thought putting a "trap 'return' 2" as the first line 
>might work, but it didn't.  Any suggestions?

You don't say, but I assume you're running on a system that has job control.
Bash puts each zcat-less pipeline in a separate process group and gives that
process group the terminal.  This means that there's a very small window for
bash to see the SIGINT at all.

The saving grace would be if either zcat or less either did nothing to the
SIGINT handler, or caught SIGINT, cleaned up, set the handler to the
default, and sent itself a SIGINT.  In either case, bash would notice that
the process died due to an interrupt and act as if it had received one
itself.  Neither does that: less efectively ignores it, and zcat catches it
and does an exit(1).

I was able to interrupt the function on my (rather slow) 4.3 BSD system by
typing ^C right after `q'uitting out of less, for what that's worth.

Chet
-- 
                    ``You shoot John Sununu.  Twice.''

Chet Ramey			  Internet: chet@po.CWRU.Edu
Case Western Reserve University	  NeXT Mail: chet@macbeth.INS.CWRU.Edu