Unix Technical Forum

How to trap any keyboard signal in a sript?

This is a discussion on How to trap any keyboard signal in a sript? within the AIX Operating System forums, part of the Unix Operating Systems category; --> Hello All, First of all, I just learn shell script and wondering how I can trap a keyboard signal ...


Go Back   Unix Technical Forum > Unix Operating Systems > AIX Operating System

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-04-2008, 10:49 PM
Bob Bale
 
Posts: n/a
Default How to trap any keyboard signal in a sript?

Hello All,

First of all, I just learn shell script and wondering how I can trap a
keyboard signal and exit out of the if loop as shown below:

myfile="/tmp/test"
if [ -f "$myfile" ]
then
cat $myfile
sleep 60
# if any key is hit before 60s, should end the if loop and go to next
command
fi
my_next_command_is_placed_here

Thanks a million,
Bob
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-04-2008, 10:50 PM
Nicholas Dronen
 
Posts: n/a
Default Re: How to trap any keyboard signal in a sript?

Bob Bale <balebob@hotmail.com> wrote:
BB> Hello All,

BB> First of all, I just learn shell script and wondering how I can trap a
BB> keyboard signal and exit out of the if loop as shown below:

BB> myfile="/tmp/test"
BB> if [ -f "$myfile" ]
BB> then
BB> cat $myfile
BB> sleep 60
BB> # if any key is hit before 60s, should end the if loop and go to next
BB> command
BB> fi
BB> my_next_command_is_placed_here

This works. Just CTRL-C during the sleep.

#!/bin/ksh
trap : 2
sleep 60
echo $0

$ ./foo
../foo <- this is output after the CTRL-C

Regards,

Nicholas

--
http://www.faqs.org/rfcs/rfc1855.html
3.1.1 General Guidelines for mailing lists and NetNews
3.1.3 NetNews Guidelines
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-04-2008, 10:50 PM
mark taylor
 
Posts: n/a
Default Re: How to trap any keyboard signal in a sript?

balebob@hotmail.com (Bob Bale) wrote in message news:<6385ed14.0403031542.2b8de33@posting.google.c om>...
> Hello All,
>
> First of all, I just learn shell script and wondering how I can trap a
> keyboard signal and exit out of the if loop as shown below:
>
> myfile="/tmp/test"
> if [ -f "$myfile" ]
> then
> cat $myfile
> sleep 60
> # if any key is hit before 60s, should end the if loop and go to next
> command
> fi
> my_next_command_is_placed_here
>
> Thanks a million,
> Bob


Check out the "trap" command, you can use this to trap signals i.e.
ctrl ^C, ctrl ^D (usually used to run a clean up function and
exit)... may not be what you are looking for ...

HTH
Mark Taylor
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-04-2008, 10:50 PM
Bob Bale
 
Posts: n/a
Default Re: How to trap any keyboard signal in a sript?

Thank you very much for all your suggestion.

I am thinking if I replace a cat command with a more command. Then I
can get out of the message any time I want. However, the system can
sit there for an hour if I am not press any key. Is there a way to do
like pausing for 60s if no key is hit then go to the next command,
instead of waiting for user to hit any key? It sounds simple, but I
do not know if it can be done or not.

Thanks,
Bob
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-04-2008, 10:51 PM
Nicholas Dronen
 
Posts: n/a
Default Re: How to trap any keyboard signal in a sript?

Bob Bale <balebob@hotmail.com> wrote:
BB> Thank you very much for all your suggestion.

BB> I am thinking if I replace a cat command with a more command. Then I
BB> can get out of the message any time I want. However, the system can
BB> sit there for an hour if I am not press any key. Is there a way to do
BB> like pausing for 60s if no key is hit then go to the next command,
BB> instead of waiting for user to hit any key? It sounds simple, but I
BB> do not know if it can be done or not.

#!/bin/ksh
trap : 2

clear; ( less /etc/hosts & ); pid=$!

sleep 60
if ps -p $pid >/dev/null 2>&1
then
kill $pid
fi

stty sane
echo "\n$0"

Again, the user should be told that they have to press CTRL-C
to continue, if they want to preempt the 60-second sleep.

Regards,

Nicholas

--
http://www.faqs.org/rfcs/rfc1855.html
3.1.1 General Guidelines for mailing lists and NetNews
3.1.3 NetNews Guidelines
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 01-04-2008, 10:52 PM
Bob Bale
 
Posts: n/a
Default Re: How to trap any keyboard signal in a sript?

Thank you very much, Nicholas. It works like a charm...Is there a way
to trap a "spacebar" key stroke signal instead of asking user to hold
down Ctrl and hit C?

Thanks,
Bob.

Nicholas Dronen <ndronen@io.frii.com> wrote in message news:<4047da80$0$200$75868355@news.frii.net>...
> Bob Bale <balebob@hotmail.com> wrote:
> BB> Thank you very much for all your suggestion.
>
> BB> I am thinking if I replace a cat command with a more command. Then I
> BB> can get out of the message any time I want. However, the system can
> BB> sit there for an hour if I am not press any key. Is there a way to do
> BB> like pausing for 60s if no key is hit then go to the next command,
> BB> instead of waiting for user to hit any key? It sounds simple, but I
> BB> do not know if it can be done or not.
>
> #!/bin/ksh
> trap : 2
>
> clear; ( less /etc/hosts & ); pid=$!
>
> sleep 60
> if ps -p $pid >/dev/null 2>&1
> then
> kill $pid
> fi
>
> stty sane
> echo "\n$0"
>
> Again, the user should be told that they have to press CTRL-C
> to continue, if they want to preempt the 60-second sleep.
>
> Regards,
>
> Nicholas

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 11:55 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com