[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

no reliable way to have SIGINT exit a script?

From: tsurara
Subject: no reliable way to have SIGINT exit a script?
Date: Mon, 9 Sep 2002 19:59:54 -0700


Probably I'm missing something, but if so, perhaps this can go in the FAQ so 
other people don't have to try all the things I've tried.

I have been looking for a reliable way of having ^C (interrupt, SIGINT) exit a 
bash script.  Most programs will just stop right there, but bash is eager and 
keeps running.  :-(

Sometimes I want to exit after doing cleanup.  But all the trap commands that I 
have tried fail to just do the command and exit.  They usually continue to do 
some commands that I don't want them to do.

Other times I just want to quit without continuing on with the next item in a 
loop or a function.  IMHO the default bash behavior should be to get out of the 
calling shell script no matter what, but perhaps there are intricacies here 
that are more pressing.

I have tried trap cmd 2 with the cmd being exit, break, kill $$, kill -1 $$, 
kill -9 $$, kill -0 $$, and a function that uses the ${nonexistent:?} construct 
in desperation.  None of them does the simple thing that I want.

I have also tried the above with cmd being something like "cleanup; trap 2; 
abovecmd".  I have tried many other variants.  Also do not work.

I searched google, google groups, tutorials, manuals, and a FAQ.  Nope.

I am not sourcing this script, just calling it.  The ^C gets done inside a 
shell function.

Why is this so hard?  What SIMPLE idiom am I missing that will let me just exit 
from a shell script, even if it is in the middle of  a loop or a subshell, 
optionally doing one thing beforehand?  Why doesn't bash just get you to the 
command prompt by default?  (And out of curiosity, why does bash not reset the 
terminal when it is interrupted in the case where a bash builtin has changed 
the terminal?)  I'm kind of curious about why there aren't answers to this all 
over the place.


2.05a.0(1)-release, ICIH.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]