LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-19-2003, 06:20 AM   #1
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

Rep: Reputation: 30
trap signals


I am trying to put some trap signals in a bash script i am working on but i'm not sure how to use it.

Has anyone got any ideas or examples?
 
Old 10-19-2003, 09:03 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Run this script. Hit CTRL-C 6 times, and see what happens, and how it works.
Code:
#!/bin/sh

count=0

# Declare function to handle CTRL-C signals
signal_func() {
    count=$((count + 1))
    echo "INT signal received $count times."
    if [ $count -ge 5 ] ; then  # After 5 INT's 
	# reset signal handling to  default.
	trap - INT
    fi
}

# Install handler for INT signals (CTRL-C)
trap 'signal_func' INT

# Loop doing nothing for demo.
while true ; do
    sleep 1000000
    echo "Woken up"
done

# we never get here.
exit 0
 
Old 10-19-2003, 01:17 PM   #3
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

Original Poster
Rep: Reputation: 30
Thats primo

Is there also a way that if they wanted to press Q that it could break the script aswell?
 
Old 10-19-2003, 04:31 PM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
To do that, you would need to run a program in the background that signals the shell executing the script when Q is pressed. I don't know if such a program exists, but it shouldn't be to difficult to make (in C or C++).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to send snmp trap & recieve trap in C program minil Programming 3 07-10-2010 09:22 AM
Kernel trap (Fatal trap 12) m!k@EL *BSD 4 09-05-2007 11:58 PM
What is trap program?? Zero-0-Effect Linux From Scratch 2 09-17-2004 09:07 PM
setting a trap antken Linux - Security 6 01-11-2003 06:45 AM
How can i trap the Console signals in C++ Code Mr_ss Programming 0 05-18-2001 03:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration