我正在尝试使用read来执行脚本中的命令,当用户使用Ctrl C时,我想停止执行命令,但不要退出脚本.
这样的事情
这样的事情
#!/bin/bash input=$1 while [ "$input" != finish ] do read -t 10 input trap 'continue' 2 bash -c "$input" done unset input
当用户使用Ctrl C时,我希望它继续读取输入并执行其他命令.问题是当我使用如下命令:
while (true) do echo "Hello!"; done;
在我输入Ctrl C一次之后,它不起作用,但是一旦输入了几次,它就会起作用.
您需要在不同的进程组中运行该命令,最简单的方法是使用作业控制:
#!/bin/bash # Enable job control set -m while : do read -t 10 -p "input> " input [[ $input == finish ]] && break # set SIGINT to default action trap - SIGINT # Run the command in background bash -c "$input" & # Set our signal mask to ignore SIGINT trap "" SIGINT # Move the command back-into foreground fg %- done
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。