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

Re: Keystrokes go missing after script exits

On Sat, Jan 3, 2009 at 11:31 PM, Curt Howland <Howland@priss.com> wrote:
> Hash: SHA1
> On Saturday 03 January 2009, James Youngman was heard to say:
>> It's left your terminal in a non-echoing mode; this is used for
>> example when asking for a password.   You can fix this with "stty
>> sane".
> Yes, I understand it's gone into non-echo mode. I just didn't know
> that word for it.
> Thanks for the after-fix, sure enough it worked.
> Now, can I put that as the last command in my script and not have the
> problem in the first place?

If it's the last command executed, yes.   But the last command
executed may not the the final command in the script; it may exit

There are three obvious ways to solve this problem.

1. Probably the most general solution: Rename the original script and
make a new wrapper script that calls it:

#! /bin/sh
# We save the old terminal mode with "stty -g" and restore it later.
# This is broadly similar to just calling "stty sane" but it copes better
# with cases like terminal setups where the 'erase' character isn't backspace.
orig_terminal_mode=$(stty -g)

# run the original script...
# ... and save its return value

# fix the terminal mode
stty $orig_terminal_mode

# exit with the same status as the original script did
exit $rv

2. Modify the original script: put "stty sane" at the end of the
script and before every "exit" command that doesn't appear in a
subshell.  If there is a chance that the original script will need
other changes, this option imposes a maintenance burden on you.

3. Wrap the original script in ( ... ) and reset the terminal mode
once it's done.  That is, edit the original script so that it looks
like the code from (1) but in place of "renamed-original-script" put
the entire text of the original script, inside parentheses (   ).
This approach has all the disadvantages of option (2) except that you
don't need to look for all the exit statements.

The reason these options are all apparently complex is that "stty
sane" will succeed (that is, exit with status 0) and so any non-zero
return value from the script would be masked if you just pasted in
"stty sane" at the end.   If the script is trying to signal failure by
exiting with a non-zero status, masking that bu changing the status to
zero would potentially be damaging (i.e. the caller would not find out
that the script had in fact failed).


Reply to: