LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-26-2005, 06:24 AM   #1
gecoool
Member
 
Registered: Feb 2005
Location: Romania
Distribution: Fedora 2
Posts: 38

Rep: Reputation: 15
catch Ctrl-C from shell script


Is it possible to catch a key combination like ctrl-c from within a shell script ? if it is possible,
please advise me how to do it !

Here is a more detailed picture of my problem:

A shell script is launching an app with certain parameters (i.e start table, and how many tables to process (step)). If i press ctrl-c, The app "knows how" to handle the ctrl-c, and saves it's temporary work where it should. but the script will launch the app with the next set of parameters (i have a while loop, which exits when it reaches the 256th table):

#!/bin/sh

nr_threads=20
start_table=0
step=16
export out=0

while [ $start_table -lt 256 ]
do
launch="./index -t:$nr_threads -b:$start_table -s:$step"
echo "$launch"
$launch
let "start_table = $start_table + $step"
done

what i want exactly, is to terminate the script itself, after a ctrl-c was pressed.
 
Old 10-26-2005, 06:51 AM   #2
fvgestel
LQ Newbie
 
Registered: Oct 2005
Location: Netherlands
Posts: 21

Rep: Reputation: 15
You can catch signals with the trap command :

trap "echo CTRL-C was pressed" 2 ( 2 is the SIGINT signal, generated by pressing CTRL-C )
 
Old 10-26-2005, 10:08 AM   #3
gecoool
Member
 
Registered: Feb 2005
Location: Romania
Distribution: Fedora 2
Posts: 38

Original Poster
Rep: Reputation: 15
ok, thanks ;-). this is the type of answer, that makes me smile ;-)
 
Old 10-26-2005, 11:27 AM   #4
koodoo
Member
 
Registered: Aug 2004
Location: a small village faraway in the mountains
Distribution: Fedora Core 1, Slackware 10.0 | 2.4.26 | custom 2.6.14.2, Slackware 10.2 | 11.0, Slackware64-13
Posts: 345

Rep: Reputation: 33
Yes you can use the trap command. The general syntax of the command is :
Code:
trap command signal
The command command is executed when the signal signal is caught
Since scripts are normally interpreted from top to bottom, so you must specify the trap command before the part of the script you wish to protect.
The signal INT (value = 2) is generally passed when you press CTRL-C. Type man signal to know more about signals.


You're saying that when you press CTRL-C the application knows what to do, i.e. it gets the signal and quits whereas the script doesn't quit and launches the application again and again with another set of parameter until the while loop is over. Right?

You want that the script should terminate whenever a CTRl-C signal is passed.


This is wierd at least to me. I tried calling another script form one script and then pressing CTRL-C, both scripts terminated for me. I don't know why only the application is terminating in your case and not the script also.


You could try something like this :
Code:
#!/bin/sh

nr_threads=20
start_table=0
step=16
export out=0

trap 'exit 0' INT or simply trap INT 

while [ $start_table -lt 256 ]
do
launch="./index -t:$nr_threads -b:$start_table -s:$step"
echo "$launch"
$launch
let "start_table = $start_table + $step"
done
If you solved the problem do let us know. Many other people might be having similar problems.
Hope this helps!

Last edited by koodoo; 10-26-2005 at 11:38 AM.
 
Old 10-27-2005, 01:19 AM   #5
gecoool
Member
 
Registered: Feb 2005
Location: Romania
Distribution: Fedora 2
Posts: 38

Original Poster
Rep: Reputation: 15
well, first thanks for such a thorough reply ;-) ! The answer if I succedeed to solve this problem is yes ;-). Here is how: when an app terminates it normally should return a 0. All I did, was to return from my application a non-zero value, which I "caught" it from the script ($? is the return code). Like this if I press CtrlC, my app finishes nicely what it was doing (i.e to process current record, not the remaining of the table), and returns 1. Now when the app exits , the next if, is going to terminate the script ;-).

#!/bin/sh

nr_threads=10
start_table=0
step=16

while [ $start_table -lt 256 ]

do
launch="./index -t:$nr_threads -b:$start_table -s:$step"
$launch
if [ $? -ne 0 ]
then
exit 1
fi
let "start_table = $start_table + $step"
done
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
How do I trigger ctrl+z via a script? closet geek Linux - General 1 05-31-2005 06:53 PM
how to have no interpretation of special characters (ctrl, tab ...) in a script xround Linux - General 1 11-29-2004 07:21 AM
C++ catch ctrl+c true_atlantis Programming 1 10-01-2004 11:23 AM
ctrl+alt+f1(shell prompt?) big problem... SpItoS Mandriva 0 05-22-2004 03:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration