考虑一下这个脚本:
#!/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"如果我以用户lars的身份运行它并输入woohoo,则得到以下输出:
hi there lars
hi there root
enter stuff:
answer: woohoo但是,如果我在脚本等待我的ctrl-c输入时使用read,那么我就进入了一个奇怪的状态。控制台似乎陷入了静默状态。如果省略-s (=静默模式)选项,则不会出现问题。
你知道这里到底有什么问题吗?如果有人在输入期间按下ctrl-c,我如何使脚本正确运行。
我正在跑快车4.3.30。
发布于 2018-08-20 08:47:51
显然,这是一个Bash4.3中的bug,Bash 4.4中的修正:
哦哦。修正了一个错误,如果在readline()调用(包括
read -e' andread -s')时收到致命信号,bash就不会清除readline的状态,包括终端设置。
我用一个恢复终端设置的陷阱来解决这个问题:
[ "`whoami`" = "root" ] || {
exec sudo -u root "$0" "$@"
}
function finish {
stty echo echok
}
trap finish EXIT
read -s -p "enter stuff: " stuff
echo "answer: $stuff"https://unix.stackexchange.com/questions/463253
复制相似问题