gpt4 book ai didi

linux - 在安装期间捕获命令行终止

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:23:27 25 4
gpt4 key购买 nike

我正在编写一个脚本,用于在我们的服务器上自动安装 ffmpeg。到目前为止它有效。为了使脚本更加用户友好,我想在脚本中引入一些东西,它使用 Ctrl+C 或 Ctrl+Z 等捕获安装的终止,并给出类似“在用户请求时终止......”之类的输出那种。问题是,我不知道脚本是否可以捕捉到这些角色。我搜索并找到了来自 here 的 Bash 内置陷阱命令。并想知道这是否是正确的方法。如果是的话,就像 control_c() 和等效的 control_z() 一样?

任何帮助表示赞赏。提前致谢

最佳答案

是的,使用 trap是要走的路

control_c()
# run if user hits control-c
{
echo -en "\n*** Ouch! Exiting ***\n"
cleanup
exit $?
}

# trap keyboard interrupt (control-c)
trap control_c SIGINT

要捕获使用Control-Z 键发送的信号,您需要捕获SIGTSTP。信号:

control_z()
{
echo -e "\n*** Suspending asked... Exiting! ***"
cleanup
exit $?
}

trap control_z SIGTSTP

关于linux - 在安装期间捕获命令行终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17403753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com