Visit Our UNIX and Linux User Community


How to generate cntl+c interrupt through script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to generate cntl+c interrupt through script?
# 1  
Old 10-29-2010
How to generate cntl+c interrupt through script?

Hi all,

can anyone tell me how to generate control+c interrupt through shell script.
# 2  
Old 10-29-2010
Code:
kill SIG

... need to find the corresponding SIG (kill -l)
Code:
kill -s INT $$

?


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

Last edited by ctsgnb; 10-29-2010 at 10:53 AM..
# 3  
Old 10-29-2010
It depends what you are trying to do....

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:-
Code:
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
Code:
trap "echo \"You hit CNTL-C\"" INT

early in the script will display the message and continue. You could grow it into
Code:
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
Code:
#!/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

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Generate documentation for a shell script

Hi, I've written a shell script with proper intentation and commenting structure. However, I would like to generate documentation for the shell which I have written. Is there any tool as such to generate it like we have javagen/docgen ? Please help. Thanks, Arjun (0 Replies)
Discussion started by: arjun_arippa
0 Replies

2. UNIX for Dummies Questions & Answers

Need script to generate file.

Hi I have a file "test" with data as below 1,APRIL,NEW,"New market,delhi,pune,India",RECENT, 254664 2,MARCH,OLD,"New Area,Mumbai,UP,India",CURRENT, 152483 So I want a script which provides output as below 1,APRIL,RECENT,254664 2,MARCH,CURRENT,152483 I am aware we can use awk/sed... (9 Replies)
Discussion started by: sv0081493
9 Replies

3. Shell Programming and Scripting

Generate random numbers in script

i want to generate a random number through a script, and even if anyone reads the script, they wont be able to figure out what the random number is. only the person who setup the script would know it. something like this could work: random the full thread is here: ... (13 Replies)
Discussion started by: SkySmart
13 Replies

4. Shell Programming and Scripting

generate a script that will do the following

I need to write a shell script that will do all this: Go to server directory, for example (/xyz/outbound) In the outbound folder, get the list of the (old) directories created or modified at least two days before the date when script is run. The list will have the names of directories, some... (2 Replies)
Discussion started by: chako_3
2 Replies

5. UNIX for Advanced & Expert Users

Is there any shell command to show which interrupt handler handle which interrupt number?

Hi, all: Is there any shell command to show which interrupt handler handle which interrupt number in the system? li,kunlun (5 Replies)
Discussion started by: liklstar
5 Replies

6. UNIX for Advanced & Expert Users

Interrupt storm detected on "irq 20" throttling interrupt source

I receive the following warning messages on a very new machine which has FreeBSD 8.1 x64 installed on it: Interrupt storm detected on "irq 20" throttling interrupt source It is unclear what this means and what its origins are (motherboard? CPU? RAM?). I can start the desktop and the message is... (4 Replies)
Discussion started by: figaro
4 Replies

7. Shell Programming and Scripting

help....generate command script

Please help..... if i have a file list below : 1233743158.log 1233743410.log 1233744186.log 1233744462.log "1233743158" is a unix time format it's require to a script "convertime.sh" to convert unix time format to readable time format like 20090204183010 -- YYYYMMDDHHMMSS ... (10 Replies)
Discussion started by: bleach8578
10 Replies

8. Shell Programming and Scripting

Cntl+z Trap is not detecting ??? Help required to add a trap detection ???

Hi folks, I have tried to add some trap detection in the below script....this script is used to monitor database activities...in a rather awkward way :rolleyes:.... The idea behind adding trap is that....this script creates lots of temporary files in the running folder to store the count... (1 Reply)
Discussion started by: frozensmilz
1 Replies

9. Shell Programming and Scripting

Sending an interrupt in a script?

Is it possible to sent a ^C interrupt via the command line? For example if I want to tail a log for 10 minutes at a time, kill the tail and then start it again is there a way to go about that? I would imagine there would be some way to do it by finding and killing the PID, but I'm curious if... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

10. UNIX for Dummies Questions & Answers

how to cntl^c in a script

can anyone please let me know how I can terminate a command Ex:"truss filename.truss.txt -p pid" after letting it run for 2sec in a korn shell script.In other words how can we emulate cntl^c in a script?? (3 Replies)
Discussion started by: Ravi Kanth
3 Replies

Featured Tech Videos