Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
google site



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Search this Thread
  #1  
Old 09-21-2005
Registered User
 

Join Date: Sep 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Disabling ctrl-Z key inside shell script

Hi
I have tried to disable the CTRL-Z key inside a shell(sh) script using the command trap "`echo "Ctrl-Z key disabled"`" 20But I am not able to exit from the script after pressing CTRL-Z key.
How to proceed this? Need reply soon
Sponsored Links
  #2  
Old 09-21-2005
RTM's Avatar
RTM RTM is offline Forum Advisor  
Registered User
 

Join Date: Apr 2002
Location: On my motorcycle
Posts: 3,090
Thanks: 0
Thanked 6 Times in 3 Posts
See Vino's suggestion below (sorry, I was in a rush and in the middle of this and real work)

Last edited by RTM; 09-21-2005 at 08:38 AM..
  #3  
Old 09-21-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,843
Thanks: 0
Thanked 2 Times in 2 Posts
Quote:
Originally Posted by RTM
Instead of 20, try 24.
Wouldn't this be better off ?


Code:
trap "echo "Ctrl-Z key disabled" SIGTSTP

From solaris signal primer

we have


Code:
SIGTSTP 	24 	Stop 	Stop (job control, e.g., ^z))

And from my linux machine


Code:
$ kill -l SIGTSTP
20
$ kill -l 20
TSTP
$ kill -l TSTP
20
$ kill -l 24
XCPU

vino

Last edited by vino; 09-21-2005 at 08:34 AM..
  #4  
Old 09-21-2005
RTM's Avatar
RTM RTM is offline Forum Advisor  
Registered User
 

Join Date: Apr 2002
Location: On my motorcycle
Posts: 3,090
Thanks: 0
Thanked 6 Times in 3 Posts
Tried the following on Solaris (2.6 and 8)

Code:
#!/bin/ksh
trap "" SIGTSTP
echo "try it - should not work"
sleep 15
trap - SIGTSTP
exit

It doesn't work - still allows Control-Z where if I change SIGTSTP to 24, it works. Tried under /bin/sh also.
  #5  
Old 09-21-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,843
Thanks: 0
Thanked 2 Times in 2 Posts
Tried this on linux and it worked.


Code:
#!/bin/ksh

trap "" TSTP
echo "try it - should not work"
sleep 15
trap - TSTP
exit

signal 24 and SIGTSTP did not work.

It didnt recognize SIGTSTP. Gave me

Code:
./rtm.ksh[3]: trap: bad signal SIGTSTP

And for 24,


Code:
$ ./rtm.ksh  
try it - should not work
[1] + Stopped              ./rtm.ksh 
$ fg
./rtm.ksh

I guess the op has to make the decision for the signal number to be used.

vino
  #6  
Old 09-21-2005
RTM's Avatar
RTM RTM is offline Forum Advisor  
Registered User
 

Join Date: Apr 2002
Location: On my motorcycle
Posts: 3,090
Thanks: 0
Thanked 6 Times in 3 Posts
TSTP worked on solaris.
  #7  
Old 09-21-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,147
Thanks: 0
Thanked 13 Times in 12 Posts
Signals in a shell script become very complex very fast. Notice that the OP complained about using:
trap "`echo "Ctrl-Z key disabled"`" 20
Using the correct quoting is not any better:
trap "echo Cntl-Z key disabled" 20

You guys are proposing:
trap "" 20
which works fine. Whole different stretch of road. Try your scripts using the above trap statement. When you get tried of waiting for them, open another window, locate that sleep process, and do a "kill -CONT" to it.

Control Z is usually the SUSP character. Typing the SUSP characters sends SIGTSTP to all processes in the terminal's foreground process group. To disable control Z, the command is:
stty susp ^-
All of the posts here deal with catching or ignoring the TSTP signal, not disabling Control Z. Remember that shell scripts are collections of processes. The shell uses fork() and exec () to run the sleep program. From the exec man page:
Quote:
The processing of signals by the process is unchanged by exec*(), except that signals caught by the process are set to their default values (see signal(2)).
So when TSTP is ignored, it stays ignored during the execution of the sleep process. But if it was caugth, it goes back to the default action which is to suspend the process. This is bad enough, but what's more, ksh has some bug involving caught job control signals. ksh has it own internal routine to catch those signals. It gets run instead of the specified command. So you don't even get the the message echoed.

I can only curse the darkness. I don't have a candle to light. Sorry.
Sponsored Links
Closed Thread

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to run an SQL script inside a shell stevefox Shell Programming and Scripting 1 06-15-2006 10:11 PM
ftp files inside a shell script matrix1067 Shell Programming and Scripting 5 02-07-2006 04:31 PM
How to run unix commands in a new shell inside a shell script? hkapil Shell Programming and Scripting 2 01-04-2006 05:56 AM
Disable ctrl-c,ctrl-d,ctrl-d in ksh script wtofu AIX 6 06-29-2005 04:41 PM
Using tar inside a shell script kas7225 Shell Programming and Scripting 2 05-19-2005 11:06 PM



All times are GMT -4. The time now is 11:19 PM.