如何防止用户使用ctrl-c停止脚本? [英] How to prevent a user from using ctrl-c to stop a script?

查看:59
本文介绍了如何防止用户使用ctrl-c停止脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将脚本加载到 .bash_profile 中,并且每当用户打开终端窗口时,此脚本都会要求输入正确的密码.如果用户输入了错误的代码,则脚本将运行 exit 来停止当前终端.

I load a script in .bash_profile and this script will ask for right password whenever a user opens a terminal window. If the user enters a wrong code, the script will run exit to stop the current terminal.

if [ $code = "980425" ]; then
    echo hello
else
    exit
fi

但是我意识到用户可以始终使用 ctrl - c 停止脚本并进入终端.如何避免呢?

But I realize that the user can always use ctrl-c to stop the script and enter the terminal. How to avoid that?

推荐答案

您始终可以捕获 SIGINT :

trap 'echo got SIGINT' SIGINT

完成后,再次使用

trap SIGINT

有关详细信息,请参见 POSIX陷阱规范.这适用于所有Bourne弹壳,而不仅仅是bash.

See the POSIX spec for trap for details. This works in all Bourne shells, not just bash.

这篇关于如何防止用户使用ctrl-c停止脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆