LinuxQuestions.org
Visit Jeremy's Blog.
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 11-16-2006, 12:11 AM   #1
procfs
Member
 
Registered: Jan 2006
Location: Sri Lanka
Posts: 651

Rep: Reputation: 34
capture ctrl+c


Hi

I have some scripts that will show me a backup options (Menu),

At the menu if some one press ctrl + c it exit and gives the prompt. I want to stop this.

if some one press ctrl + c I want to logout from the system. How ca I do this.

I am using Redhat ES3 update 8


Thank you

Regards

Asanka

Last edited by procfs; 11-16-2006 at 12:16 AM.
 
Old 11-16-2006, 07:37 AM   #2
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
I'm not sure I understand what you're trying to do.
Maybe that "someone" should exec your script from the login shell?

OTOH, you mention you have "scripts" -- what scripting language? (E.g., bash, perl, tcl, ...) If it's bash, and you want to do something specific in response to ctrl+c, and , see man bash, particularly the trap builtin. You'll want SIGINT as the sigspec.

You need to tell us more.
 
Old 11-17-2006, 05:54 AM   #3
procfs
Member
 
Registered: Jan 2006
Location: Sri Lanka
Posts: 651

Original Poster
Rep: Reputation: 34
Hi Quigi

There are some bash shell scripts that I use to backup my databases.

Thing is while the user is in the menu if he press control c he will be taken to the shell prompt. I want to stope this I wan is if the user press Control C he must be logged out.

Is there a way to do this.

Thanks

Regards

Asanka
 
Old 11-17-2006, 08:46 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
exec your_script
 
Old 11-17-2006, 08:47 AM   #5
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
Quote:
Originally Posted by asankacgl
Here are some bash shell scripts that I use to backup my databases.

I want to stope this I wan is if the user press Control C he must be logged out.
(emphasis mine)

Please explain what you're trying to achieve. Who is the user? Not you? Somebody that walks up to your terminal while you are using some bash shell scripts, and hits C-c to gain access to your shell?

Quote:
Is there a way to do this.
Yes.
 
Old 11-17-2006, 09:23 AM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
In a bash script, you can trap a signal and handle it with a function. When ^C is pressed in a terminal, the SIGINT signal is sent to the shell, so we can trap this. bash also sets an environment variable PPID which is the PID of the parent process, so you can send a signal to this in your signal handler. SIGHUP would be a good one, requesting that the tty hang up.

In this script you can use ^C to kill the parent shell. The while loop is just an annoying example menu which never exits.
Code:
#!/bin/bash

kill_parent () {
    echo 'death to my parent!'
    sleep 3
    kill -HUP $PPID
}

trap kill_parent INT

while true; do
    echo "Options:"
    echo "1. Say hi"
    echo "2. Say lo"
    echo "3. Say hi ho"
    echo -n "your choice? > "
    read answer
    case $answer in
    1)
        echo "hi"
        ;;
    2)
        echo "lo"
        ;;
    3)
        echo "hi ho"
        ;;
    *)
        echo "what?"
        ;;
    esac
done
 
Old 11-17-2006, 08:56 PM   #7
procfs
Member
 
Registered: Jan 2006
Location: Sri Lanka
Posts: 651

Original Poster
Rep: Reputation: 34
Hay gus thank you for your help

Hay I was able to do this by using some thing like

trap 'echo "Control-C disabled."' 2

Best regards

Asanka

Last edited by procfs; 07-26-2007 at 01:22 PM.
 
  


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
ctrl+c & ctrl+v equivalent for Gnome terminal window? halfpower Linux - General 2 11-29-2005 02:57 AM
dark screen when pressing Ctrl + Alt + Backspace or Ctrl + Alt F1-F6 in Gnome or KDE trinoo Slackware 5 01-28-2005 08:13 AM
no ctrl-a or ctrl-z in emacs or on command line? ashlock Fedora 5 12-30-2003 08:00 PM
ctrl+ c ctrl +v not working in windows xp Astro General 3 10-20-2003 08:42 PM
Capture support for dvd and video capture in linux is it even going to be real ever? maximalred Linux - Distributions 3 07-06-2003 07:29 PM

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

All times are GMT -5. The time now is 05:14 AM.

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