How to interrupt shell functions?

Chet Ramey chet at thor.INS.CWRU.Edu
Sun Jun 23 13:24:29 AEST 1991


In article <1991Jun21.002944.11023 at brolga.cc.uq.oz.au> eric at 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 at po.CWRU.Edu
Case Western Reserve University	  NeXT Mail: chet at macbeth.INS.CWRU.Edu



More information about the Comp.unix.shell mailing list