help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Restoring tty line settings after a trap


From: Greg Wooledge
Subject: Re: [Help-bash] Restoring tty line settings after a trap
Date: Tue, 17 Jun 2014 08:00:53 -0400
User-agent: Mutt/1.4.2.3i

On Mon, Jun 16, 2014 at 05:27:55PM -0700, Jesse Molina wrote:
> I have an issue with a bash script where if the user calls SIGINT via a 
> ctrl+c, my trap gets called and the tty line settings don't get restored 
> like they should, because they had been changed before trap was called, 
> but not restored.
...
> Is there are a "better" way to do this, or is doing an stty save and 
> restore my best bet?

Using stty to reset the terminal to a sane state is probably the best
approach.  If you don't need to handle each signal specially, then you
can just do:

cleanup() {
    # remove temp files and so on
    stty sane
}
trap 'cleanup' EXIT

However, if you explicitly catch SIGINT, then your script should
kill itself with SIGINT, not just exit, because SIGINT is special.
See http://www.cons.org/cracauer/sigint.html for details.

In that case, something like this may be required:

cleanup() {
    # remove temp files and so on
    stty sane
}
trap 'cleanup; trap - INT; kill -INT $$' INT
trap 'rc=$?; cleanup; exit $rc' HUP TERM



reply via email to

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