0

Consider this script:

#!/bin/bash

echo "hi there $(whoami)"

[ "`whoami`" = "root" ] || {
  exec sudo -u root "$0" "$@"
  echo "this is never called"
}

read -s -p "enter stuff: " stuff
echo "answer: $stuff"

If I run it as user lars and enter woohoo, then I get the following output:

hi there lars
hi there root
enter stuff:
answer: woohoo

However, if I ctrl-c while the script is waiting for my read input, then I get into a weird state. The console seems to be stuck in silent mode. The problem does not occur if I omit the -s (= Silent mode) option.

Do you have any idea what the exact problem is here? How can I make the script behave properly if someone presses ctrl-c during the input.

I am running bash 4.3.30.

2
  • Can not reproduce.
    – user232326
    Aug 17, 2018 at 22:01
  • @Isaac I just replicated the behavior on a Amazon ECS machine with Ubuntu 14.04.5 LTS (trusty) and BASH 4.3.11(1)-release (x86_64-pc-linux-gnu). On what environment did you test? Aug 19, 2018 at 15:56

1 Answer 1

1

Apparently this is a bug in Bash 4.3 that was fixed in Bash 4.4:

oo. Fixed a bug that caused bash to not clean up readline's state, including the terminal settings, if it received a fatal signal while in a readline() call (including `read -e' and `read -s').

I worked around the problem with a trap that restores the terminal settings:

    [ "`whoami`" = "root" ] || {
      exec sudo -u root "$0" "$@"
    }

    function finish {
      stty echo echok
    }
    trap finish EXIT
    read -s -p "enter stuff: " stuff
    echo "answer: $stuff"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .