ctrl c trapping signal


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ctrl c trapping signal
# 1  
Old 11-30-2011
ctrl c trapping signal

im trying to make a trap signal 2 (ctrl c) in a bash script

if a user presses ctrl c while running the script it should display an error message but not quit the bash script just yet. User will have to press "enter" to quit

This is what i have so far

Code:
#trap trap_control 2
#while true
#do
#echo "You press ctrl C"
#done
#clear

# 2  
Old 11-30-2011
Code:
#! /bin/bash

trap_ctrlC() {
    echo '<CTRL-C> has been trapped'
    read -p 'Enter to exit: ' line
    exit 1
}

trap trap_ctrlC INT

while true; do
    echo '<CTRL-C> to quit'
done

This User Gave Thanks to m.d.ludwig For This Post:
# 3  
Old 11-30-2011
Quote:
Originally Posted by m.d.ludwig
Code:
#! /bin/bash

trap_ctrlC() {
    echo '<CTRL-C> has been trapped'
    read -p 'Enter to exit: ' line
    exit 1
}

trap trap_ctrlC INT

while true; do
    echo '<CTRL-C> to quit'
done

nothing happens when i press control c? It justs exits out no message is displayed
# 4  
Old 11-30-2011
Are you running this as a script?
Or directly on the command line?
# 5  
Old 11-30-2011
its in a script, and i replaced all the ' with "

this is what im trying

Code:
function trap_ctrlC()
{
    echo "Exited."
    read -p "Press Enter to exit " exitS
    exit 0
}

trap trap_ctrlC 2

# 6  
Old 11-30-2011
Here is my results:
Code:
ludwig$ bash x
<CTRL-C> to quit
<CTRL-C> to quit
<CTRL-C> to quit
<CTRL-C> to quit
<CTRL-C> to quit
.
.
.
<CTRL-C> to quit
<CTRL-C> to quit
<CTRL-C> to quit
<CTRL-C> to quit
<CTRL-C> to quit
<CTRL-C> to quit
<CTRL-C> to quit
<CTRL-C> to quit
<CTRL-C> to quit
^C<CTRL-C> has been trapped
Enter to exit: 
ludwig$

And I did replace the single-quotes with double-quotes.
This User Gave Thanks to m.d.ludwig For This Post:
# 7  
Old 12-01-2011
edit i got it working thnks

after the user presses how would i "clear" the screen" so only the error messages are displayed?

trying to figure out where to put clear

Last edited by gangsta; 12-01-2011 at 11:43 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Send ctrl-C signal using bash script.

declare -a array=( "LLC-load-misses" "LLC-loads" "LLC-store-misses" "LLC-stores" "branch-load-misses" "branch-loads" "dTLB-load-misses" "dTLB-loads" "dTLB-store-misses" "dTLB-stores" "iTLB-load-misses" "iTLB-loads" "branch-instructions" "branch-misses" "bus-cycles" "cache-misses" "cache-references"... (2 Replies)
Discussion started by: BHASKAR JUPUDI
2 Replies

2. Shell Programming and Scripting

Automation of keyboard inputs..like Ctrl+d and Ctrl+a

Hi..! I'm stuck with my automation of starting a process and keeping it running even after the current ssh session has exited.. So i'm trying to use command 'screen'. which is doing exactly what i wanted, But the problem is automation of the same. i will have to press Ctrl+a and Ctrl+d for... (2 Replies)
Discussion started by: chandana hs
2 Replies

3. Shell Programming and Scripting

Vi : Is it possible to send ctrl + d signal from a file made with vi and executing it.

Hi Experts, Is it possible to send ctrl + d signal from a inside a file made with vi, using Ctrl V , Esc and 004 , escape sequence. Since : 004 should exit the script if executed. Is this something possible. I am trying with vi , I put this code ^ , and trying to execute it but... (4 Replies)
Discussion started by: rveri
4 Replies

4. UNIX for Dummies Questions & Answers

Ctrl-V + Ctrl-J for newline character does not work inside vi editor

Hi friends, I am trying to add a newline char ('\n') between the query and the commit statement in the following shell script. #! /bin/sh echo "select * from tab; commit;" > data.sql I have tried typing in "Ctrl-V + Ctrl-J" combination which has inserted ^@ (NUL) character but the commit... (1 Reply)
Discussion started by: royalibrahim
1 Replies

5. Shell Programming and Scripting

How to handle CTRL+Z or CTRL+C in shells script?

Hi, while executing shell script, in the middle of the process, if we kill the shell script( ctrl+z or ctrl+c), script will be killed and the files which using for the script will be in the folder. How to handle those scenarios. Is there any possibilities, if user breaks the script, I need to... (3 Replies)
Discussion started by: ckchelladurai
3 Replies

6. Shell Programming and Scripting

Ctrl-C or Ctrl-Z causing exit the session

H! I have written script where it need to invoke the perl script in background, then write the pid in temp file then bring back the job to foreground. whenever the Ctrl-C or Ctrl-Z is pressed in the script has to exit and prompt should be dispalyed. but this script causing exit from shell session... (2 Replies)
Discussion started by: jramesh1
2 Replies

7. Shell Programming and Scripting

How to continue in the loop after trapping signal

hi all.... plz tel me how can i solve this....here's the situation (just a sample!!).. $ cat sigtrap #!/usr/bin/perl $SIG{'INT'} = 'ABORT'; sub ABORT { print "\nStop the loop?? (y/n) : "; chop($ch=<STDIN>); if ($ch =~ //) { ... (3 Replies)
Discussion started by: tprayush
3 Replies

8. Shell Programming and Scripting

Catching ctrl-C or ctrl-D

Hi there, I'm using HP-UX 11 machine. I am running a script, thats gonna take a long time to execute. When I press ctrl-c to come out of my script, I have to catch that signal(ctrl-c) and display that ctrl-c had been pressed. How can I do it. Thanks in advance (2 Replies)
Discussion started by: sendhilmani123
2 Replies

9. Shell Programming and Scripting

Ctrl-C signal propagation

Hello I have a master startup script (let's call it myScript) that displays a menu from which the user can start/stop several instances of a server. When I issue the start command for one of the servers from the menu and then exit myScript through the provided mechanism (enter "q" in this case),... (2 Replies)
Discussion started by: EvilBoz
2 Replies

10. AIX

Disable ctrl-c,ctrl-d,ctrl-d in ksh script

I wrote a ksh script for Helpdesk. I need to know how to disable ctrl-c,ctrl-z,ctrl-d..... so that helpdesk would not be able to get to system prompt :confused: (6 Replies)
Discussion started by: wtofu
6 Replies
Login or Register to Ask a Question