Computing.Net > Forums > Unix > Exit routine for ksh !

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Exit routine for ksh !

Reply to Message Icon

Name: abby
Date: July 15, 2002 at 11:49:23 Pacific
Comment:

Hi,
I'm trying to write an exit routine for ksh script which ideally should capture the user input "control c" and then exit the script gracefully.
Any ideas and suggestions ?
Thanks,



Sponsored Link
Ads by Google

Response Number 1
Name: LANkrypt0
Date: July 15, 2002 at 12:05:10 Pacific
Reply:

Control C should cancel out of the script itself.


0

Response Number 2
Name: James Boothe
Date: July 16, 2002 at 06:57:01 Pacific
Reply:

If a script is at a certain critical stage where a user cancel might leave things in a bad state, the script will want to disable the user cancel request for this portion of the script. Also, I have some scripts that make use of temporary files. If the user cancels, I do honor the cancel request, but I intercept it so that I can do some house cleaning first.

You can define what you want to happen when a certain signal is detected via the trap command. The trap command watches for a certain signal, not a certain key. Signal 2 is interrupt signal, usually mapped to Ctrl-C. If the trap code does not exit, the script will continue its execution after the trap executes. Run the following script and try to cancel it:

#!/bin/ksh
trap 'echo Ouch' 2
sleep 3
sleep 3
sleep 3
sleep 3
sleep 3
sleep 3
exit 0

Following is a signal 2 trap that does some stuff and then exits. Just for testing, it exits with status code 3, but could be zero or whatever:

trap 'echo "You pressed interrupt key";\
echo "doing some cleanup";\
exit 3' 2


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Exit routine for ksh !

Environment commands for KSH www.computing.net/answers/unix/environment-commands-for-ksh/6476.html

fail exit status for telnet script www.computing.net/answers/unix/fail-exit-status-for-telnet-script/6524.html

unix script exit status www.computing.net/answers/unix/unix-script-exit-status/7403.html