How to generate cntl+c interrupt through script?

Hi all,

can anyone tell me how to generate control+c interrupt through shell script.

kill SIG

... need to find the corresponding SIG (kill -l)

kill -s INT $$

?

break (get out of a loop)
or
exit (get out the script)

Are you trying to terminate another process? If so, use kill and look and the values to set in the man pages for signal.

Are you trying to recognise a user pressing CNTL-C? If so, you have a few options:-

stty -isig

This will ignore all interupts from the keyboard. This may be a little heavy handed though, so you could trap the CNTL-C and handle it in your script. The line

trap "echo \"You hit CNTL-C\"" INT

early in the script will display the message and continue. You could grow it into

trap "echo \"`date` You hit CNTL-C\"" INT

You can also use the command (the bit in quotes) to call a function or do something else, but that is for you to decide what logic you want to perform such as

#!/bin/ksh
cheeky()
{
echo "You hit CNTL-C trying to break out of my code you cheeky thing."
echo "I'm going to reduce your salary by �10."
((salary=$salary-10))
}

trap "cheeky" INT

..... and the main logic flows here

You may need to get the variables exported if your wish to have a function do something, but I hope that this gives you a start.

Robin
Blackburn/Liverpool
UK