- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- CTRL-C trap in shell script
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-17-2000 01:40 AM
тАО10-17-2000 01:40 AM
Is ther way to trap ctrl-c in a shell script so that nosy, whacky, smart, silly users dont shell out to a place they should not be ?
I mean I dont want them to feel lost in that almost blank green screen!!!!!.
Thanks for any help.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-17-2000 01:42 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-17-2000 01:52 AM
тАО10-17-2000 01:52 AM
Re: CTRL-C trap in shell script
trap " echo BREAK ; exit " 2
federico
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-17-2000 02:07 AM
тАО10-17-2000 02:07 AM
Re: CTRL-C trap in shell script
...or if you prefer for the user not to see anything, but merely want to disable the Cntl_C interrupt, put this in your script:
trap ''INT
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-17-2000 02:21 AM
тАО10-17-2000 02:21 AM
Re: CTRL-C trap in shell script
Federico and Andreas are right, but with the 'exit' in the trap command the script will still exit.
So, remove the exit and your users will not be able to interrupt the script using Ctrl-C.
Bye,
Rik.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-17-2000 02:25 AM
тАО10-17-2000 02:25 AM
Re: CTRL-C trap in shell script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-17-2000 02:34 AM
тАО10-17-2000 02:34 AM
Re: CTRL-C trap in shell script
In this shell script I want to give users to exit from the terminal session.Out to the password prompt.
Now when I give 'exit' it does not logout.
is there another command for this ?
I type exit and it logs me out .
But in a shell script it does not react the same way.
Thank you again. It is a nice sunny day out here in Richmond. I hope this is same for all of you.
regards,
nalin.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-17-2000 02:37 AM
тАО10-17-2000 02:37 AM
Re: CTRL-C trap in shell script
If you assign for any of the signal keys a subroutine reference to the %SIG hash you can write your own signal handlers much better than what trap can offer.
See the POD "perldoc perlvar" for examples.
But be cautious, I read in the "Perl Cookbook" that handling system signals through ones own routines can be perilous.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-17-2000 03:14 AM
тАО10-17-2000 03:14 AM
Re: CTRL-C trap in shell script
when putting the script as the login shell in /etc/passwd exit will logout the user.
If you want to logout at shell level you can kill the session process of the user within you script with:
LOGOUT=`who am i -T|awk '{print "kill -9 " $(NF-1)}'`
trap "echo logout;$LOGOUT" 2
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-17-2000 03:52 AM
тАО10-17-2000 03:52 AM
Re: CTRL-C trap in shell script
Put trap in $HOME/.profile.
There is a patch for ksh and trap, is you use ksh.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-17-2000 04:30 AM
тАО10-17-2000 04:30 AM
Re: CTRL-C trap in shell script
Thanks for your input. I have the login sscipt in each person's .profile. Do I use vi to edit the etc/password file ? What will then happen to the stuff in the .profile for eah user ? do I delete it ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-17-2000 04:31 AM
тАО10-17-2000 04:31 AM
Re: CTRL-C trap in shell script
Include the following in the users .profile and also the /etc/profile file:
[(trap "" 1 2 3 9 15) and stty intr "^C" or stty intr "".]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-24-2001 11:04 AM
тАО02-24-2001 11:04 AM
Re: CTRL-C trap in shell script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-24-2001 03:49 PM
тАО02-24-2001 03:49 PM
Re: CTRL-C trap in shell script
trap "" 1 2 3
and at the end
trap 1 2 3
This prevents anyone from exiting from the login shell during /etc/profile, a good thing as you can use /etc/profile to deny access to a particular user by exiting to return to the login/password prompt. This trap affects the login shell only.
Just a note about turning traps on/off: In the /etc/profile example, trap "" 1 2 3 says: "Do nothing for signals 1 2 3 (also known as SIGHUP SIGINT SIGQUIT) if they occur during this script's execution."
Now, if you leave off the last line (trap 1 2 3) which reenables the traps to normal behavior, things will seem OK unless your session is aborted with a hangup (disconnect). Now your shell can't respond as it was told to ignore SIGHUP so it runs in a loop forever as there is controlling tty.
In your .profile, you can also disable traps the same way. And if you take an early exit from .profile, you will be returned to the login/password prompt.
However, if you run another script from .profile, you are no longer in your logoin shell, but a subshell so when you exit, you return back to your parent shell.
A solution is to run your script in the parent shell. This is done with 'sourcing' your script using the . (dot) command. Instead of running your script in .profile with just the script's filename, put a dot space in front of it as in:
. /usr/contrib/bin/myscript
This tells the shell to read each line as if it was typed in at the keyboard. Now an exit in myscript will tell the login shell to exit.
All of this assumes standard /usr/bin/sh (the POSIX shell). If your users insist on using strange shells (like csh) then you'll have to become familiar with their login process(like /etc/csh.login) and how to handle traps in those shells.
Bill Hassell, sysadmin