sow*_*rov 4 linux command-line trap
每次exit在命令提示符中键入命令时,我都试图获得确认消息。为此,我尝试trap在.bashrc文件中使用,但似乎陷阱不是解决方案,因为它无论如何都运行原始命令。有没有办法让我拥有这个?
这是我无法完成工作的 bashrc 脚本代码:
function _exit() # Function to run upon exit of shell.
{
read -p "${RED}Are you sure? " REPLY
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo -e "${RED}Bye${NC}"
exit 0
else
#I do not know what to do here to not exit
return
fi
}
trap _exit EXIT
Run Code Online (Sandbox Code Playgroud)
如果外壳是zsh或bash(尽管不在sh模式下),则创建exit一个函数。函数exit在zshor bash(虽然不是在 POSIX shell 中)优先于 shell 内置函数(即使是特殊的,比如)。因此,只需将您的函数重命名为exit并command exit在函数内使用即可。否则,你当然会有无限的递归。