1832860 Members
2973 Online
110048 Solutions
New Discussion

Re: Capturing CTRL + C

 
SOLVED
Go to solution
Amrit_1
Advisor

Capturing CTRL + C

Hi,

I would like to catch the signal ctrl + C(SIGINIT). Please could you let me know the corresponding value for this.

Also, I would be following the approach that needs to be implemented should be as follows: trap "numeric value". Is this the correct approach
4 REPLIES 4
Hoefnix
Honored Contributor
Solution

Re: Capturing CTRL + C

trap 2 would do the job
Mark Grant
Honored Contributor

Re: Capturing CTRL + C

Nearly,

If you are talking about a shell script it would be

trap "command" 2

The command will be executed when signal 2 is received. If you leave it blank (you still need the quotes) then the script will ignore SIGINT.
Never preceed any demonstration with anything more predictive than "watch this"
Hoefnix
Honored Contributor

Re: Capturing CTRL + C

Yep, mark is correct. The command should be filled in. If you want to use for not braking out of a script use: trap "" 2
Then it would perform your script without braking at control-C.
Tor-Arne Nostdal
Trusted Contributor

Re: Capturing CTRL + C

It depends on which shell is used.

trap is correct for korn/bourne/posix shell, but in csh you use onintr.

#!/usr/bin/csh
onintr CTRLC

LOOP:
echo 'Continue Until \c'
read DUMMY
goto LOOP

CTRLC:
echo "You pressed Ctrl+C"
exit
--------------------------------------
#!/usr/bin/sh
trap CTRLC 2
function CTRLC
{
print "You pressed Ctrl+C"
exit
}

while true
do
print 'Continue Until \c'
read DUMMY
done

/Tor-Arne
I'm trying to become President of the state I'm in...