3

Run the below script
Press Ctrl+C
Observe the current terminal behaviour.
Press enter few times and try to execute some commands.

#!/bin/bash
LOCK_FILE=/tmp/lockfile
clean_up(){
  # Perform program exit housekeeping
  echo -e "Signal Trapped, exiting..."
  # Do some Special operation
  rm -f $LOCK_FILE
  #
  exit 1
}

touch LOCK_FILE
trap clean_up SIGHUP SIGINT SIGTERM
read -s -p "Password: " var
echo -e "\n  Input Password is: $var\n"

I wonder what is the mistake I am doing?
I try to do a clean exit. It is working but after exit terminal STDIN vanishes.

Jolta
  • 2,620
  • 1
  • 29
  • 42
Dipankar
  • 141
  • 2
  • 9
  • You haven't said what you expect this to do or what it does on your machine. – l0b0 Jul 07 '14 at 13:44
  • @l0b0 actually, I've tested it and it is quite weird. On traping the SIGINT, it exits the program and messes up your shell (no character appearing when you write a command, no newline when you press enter). This is fixed when you type reset. I have no clue whatsoever why his script does that, though – Aserre Jul 07 '14 at 13:53
  • `LOCK_FILE` isn't assigned, for one thing... – twalberg Jul 07 '14 at 14:47
  • Hi, please verify now, I have modified a bit. – Dipankar Jul 09 '14 at 04:59

1 Answers1

3

read -s disables local echo (as per the documentation) if you ctrl-c out of that read it fails to reset the terminal modes for local echo. Compare the output from stty -a before and after the interrupted read to see the changes it made (look at the echo* modes).

You can use reset (as per Plutox's comment) or manually re-enable the local echo modes to "fix" the problem.

$ stty -a
speed 38400 baud; rows 46; columns 80; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts -cdtrdsr
-ignbrk brkint ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff
-iuclc -ixany imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
echoctl echoke
$
$ cat t.sh
#!/bin/bash
clean_up(){
  # Perform program exit housekeeping
  echo -e "Signal Trapped, exiting..."
  # Do some Special operation
  rm -f $LOCK_FILE
  #
  exit 1
}

trap clean_up SIGHUP SIGINT SIGTERM
read -s -p "Password: " var
echo -e "\n  Input Password is: $var\n"
$
$ sh t.sh
Password: Signal Trapped, exiting...
# I ran `stty -a` here but the lack of local echo means it didn't show up.
$ speed 38400 baud; rows 46; columns 80; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts -cdtrdsr
-ignbrk brkint ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff
-iuclc -ixany imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten -echo echoe -echok -echonl -noflsh -xcase -tostop -echoprt
echoctl echoke
Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • +1. I was about to write an answer myself :) and if you want to not show the (really) verbose output of `stty -a`, simply add `stty -a > /dev/null` right before your `exit 1` in the `clean_up()` function – Aserre Jul 07 '14 at 14:14
  • 1
    @Ploutox `stty -a` doesn't change the settings it just displays them. Adding that to the `clean_up` function wouldn't do anything to solve the problem. Adding a `reset` would but would have other side-effects as well. Manually re-enabling the `echo` and `echok` settings with `stty echo echok` will fix the problem though. – Etan Reisner Jul 07 '14 at 14:58
  • Have you tried ? It works fine for me when using it inside the function – Aserre Jul 07 '14 at 14:59
  • @Ploutox I hadn't because I knew I didn't need to but just to be sure I tried it now and no, it doesn't do anything to help. If you interrupt the `read -s` with `ctrl-c` nothing resets the modes and something needs to do that (and `stty -a` doesn't do that). – Etan Reisner Jul 07 '14 at 15:16
  • @Ploutox Me too, it doesn't help. I am remembering one approach, like save your Terminal Behaviour in a varible and restore while trap signal. But can't able to recall. – Dipankar Jul 09 '14 at 06:13
  • @Dipankar sorry about the misleading comments. After Etan's answer I reviewed my code and found out the behaviour of my script changed because of a syntax error. I should have mentioned it in a new comment though :( However, as Etan mentioned, writting `stty echo echok` right before the exit statement will restore the normal behaviour of your shell – Aserre Jul 09 '14 at 07:48
  • @Dipankar I think you are thinking about `c=$(stty -g); ...; stty "$c"`. – Etan Reisner Jul 09 '14 at 14:36
  • @Ploutox/@Etan Reisner Really you guys helped a lot, Thanks !! – Dipankar Jul 16 '14 at 12:28