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 08-29-2006, 04:12 PM   #1
BigglesZX
LQ Newbie
 
Registered: Mar 2004
Location: London, UK
Distribution: Debian
Posts: 14

Rep: Reputation: 0
Smile ^C in Shell Script


Hi everyone,

Just a quick question really - I've written a shell script that lets the user pick from a list of interactive programs to launch, and in some if ^C is keyed, control will return to my script, in others the script exits. Is there any way to ensure that any keying of ^C will affect only the called program and not my script?

Many thanks,

Biggs
 
Old 08-29-2006, 04:29 PM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
You'll need to trap the ctrl-c (and any other signal that you want to control) in your script. For example:

Code:
#!/bin/bash

function CLEANUP () {
   echo
   echo "ignoring signal"
   echo
   return
}

trap CLEANUP INT
trap CLEANUP HUP
trap CLEANUP QUIT
trap CLEANUP USR1
trap CLEANUP TERM

while true
do
   echo -n "what? "
   read x
   if [ "$x" == "q" ]
   then
      exit
   else
      echo "I got $x"
   fi
done
 
Old 08-29-2006, 04:41 PM   #3
BigglesZX
LQ Newbie
 
Registered: Mar 2004
Location: London, UK
Distribution: Debian
Posts: 14

Original Poster
Rep: Reputation: 0
Smile

That's great, thank you so much . Am I right in thinking that ^C is SIGINT?
 
Old 08-29-2006, 04:56 PM   #4
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Yes - easy to prove... just comment out the other 'trap' statements and verify.
 
Old 08-29-2006, 05:01 PM   #5
BigglesZX
LQ Newbie
 
Registered: Mar 2004
Location: London, UK
Distribution: Debian
Posts: 14

Original Poster
Rep: Reputation: 0
Nice one . Thanks again!
 
  


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 inside shell script treotan Linux - General 4 02-19-2009 06:34 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
I made a shortcut to a shell script and it is using default shell icon... shlinux Linux - Software 2 04-20-2006 06:29 AM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 02:34 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM

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

All times are GMT -5. The time now is 07:38 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