How to use Ctrl+C to stop whole script not just current command

寵の児 提交于 2020-12-29 09:04:37

问题


I have a script such as follows:

for ((i=0; i < $srccount; i++)); do
    echo -e "\"${src[$i]}\" will be synchronized to \"${dest[$i]}\""
    echo -e $'Press any key to continue or Ctrl+C to exit...\n' 
    read -rs -n1
    rsync ${opt1} ${opt2} ${opt3} ${src[$i]} ${dest[$i]}
done

If I press Ctrl+C in response to read command, the whole script will stop, but if press Ctrl+C while rsync command is running, just current rsync command will stop and the script will continue the for loop.

Is there any way to tell the script if the user pressed Ctrl+C while rsync is running, stop rsync and exit from the script itself?


回答1:


Ctrl+C sends the interrupt signal, SIGINT. You need to tell bash to exit when it receives this signal, via the trap built-in:

trap "exit" INT
for ((i=0; i < $srccount; i++)); do
    echo -e "\"${src[$i]}\" will be synchronized to \"${dest[$i]}\""
    echo -e $'Press any key to continue or Ctrl+C to exit...\n' 
    read -rs -n1
    rsync ${opt1} ${opt2} ${opt3} ${src[$i]} ${dest[$i]}
done

You can do more than just exiting upon receiving a signal. Commonly, signal handlers remove temporary files. Refer to the bash documentation for more details.



来源:https://stackoverflow.com/questions/32145643/how-to-use-ctrlc-to-stop-whole-script-not-just-current-command

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!