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 ...
| |||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| 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 |
| |||
| 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 |
| |||
| 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 |
| |||
| 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 |
| |||
| 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 |
| ||||
| 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 |