3

以下のスクリプトを実行します
Ctrl+C を押します
現在の端末の動作を観察します。
Enter キーを数回押して、いくつかのコマンドを実行してみます。

#!/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"

私は何をしているのだろうか?
私はきれいな出口を作ろうとします。動作していますが、終了後、ターミナル STDIN が消えます。

4

1 に答える 1

3

read -sctrl-cローカルエコーの端末モードをリセットできない場合、ローカルエコーを無効にします(ドキュメントに従って) 。stty -a中断された読み取りの前後の出力を比較して、行われた変更を確認します (echo*モードを見てください)。

reset(Plutoxのコメントに従って)使用するか、ローカルエコーモードを手動で再度有効にして、問題を「修正」できます。

$ 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
于 2014-07-07T14:08:12.880 に答える