bash trap ctrl c and continue to next function

80 Views Asked by At
#!/usr/bin/env bash

# trap ctrl-c and call ctrl_c()
trap ctrl_c INT

function ctrl_c() {
    echo
    echo "** Trapped CTRL-C: Skipping Current Task **"
    echo
}


function ping1 () {
    sudo ping 8.8.4.4
    sudo ping 1.1.1.1
}
function ping2 () {
    sudo ping 8.8.8.8
}
function ping3 () {
    sudo ping 8.8.4.4
}

ping1
ping2
ping3

My issue is that when I do ctrl c, it stops only the line being executed and I want it to skip the function as a whole and continue to the next function

An example is when I'm in ping1 and the first ping command sudo ping 8.8.4.4 is being executed. When I press control C, I want the script to proceed to ping2 instead of doing sudo ping 1.1.1.1

0

There are 0 best solutions below