Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

define trap signals

Status
Not open for further replies.

cruel

Programmer
Aug 6, 2001
131
Since I have a long input-soliciting process, I am thinking:

1) define a new trap signal, say, ctrl-s, when intercepted, the script will save all the current inputs to an external file.
2) I have already had a normal trap signal, say when users hit ctrl-c, the script will do some cleaning stuff. So, there will some kind of conditional trap execution.

Thanks for any tips.
 
Hello Cruel,
Your trap thinking should work, but do you only want to save on a ^S signal or most any signal that would abort the script?
I suggest that you should consider the save as a normal interrupt action during the input process. It would look like this:

trap1 () {
# ^C trap
cleanup_stuff_goes_here
exit 0
}
trap2 () {
# Input process trap
save_commands_go_here
cleanup_stuff_goes_here
exit 0
}
trap "trap1" 2
#Normal processing commands
.
.
.
# Done with normal stuff so change to input processing trap2
trap "trap2" 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# Input Processing Commands
.
.
.
# End of Input Processing so turn off trap2
trap 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# Now return to trap1
trap "trap1" 2
#More Normal Commands
.
.
trap 2
exit

This is how it works in Bourne anyway. You can set the trap to whatever function you want when you need it and then change it again later.

You might want to change the processing so you can continue. In this case, change the exit in the trap function to a return. I believe that the command the trap was invoked on is aborted and processing will continue on the next line.
Later!
Charlie Ehler
 
Hi charlie,
save when abort is what I have currently. I see it would make good sense but users sometime feel it is not secure enough without saving frequently.

Trap Signals 16 and 17 (not on all machines) allow users to define trap signals. It goes like this
signal=16
trap (funk) $signal
where funk is what will be done when user-defined signal received. I tried normal characters, it works, but never figure out how to define a signal preceding with ctrl.

but I like the idea of setting two trap functions. thanks
 
Cruel,
So right. I normally use 16 or 17 to set up a child process that kills a read after a period of time. It works great for lists that need to refresh, but you might also want user input.
I have never tried to define a kill signal(never had the need), but included them in case you might be using them.
Later!
Charlie Ehler
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top