trap CTRL-C problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting trap CTRL-C problem
# 1  
Old 02-15-2010
trap CTRL-C problem

I am trying to trap CTRL-C, now the program I call has it's own exit message, I think this is the problem ..

This is what I have now :

Code:
function dothis
{
echo 'you hit control-c'
exit
}

function settrap
{
trap dothis SIGINT
}
settrap
until false; do
./ITGRecv.exe
done

Doing this I am able to trap it but only after pressing CTRL-C twice, the first time I press it it just runs my program again.
Could also be because of the until statement that runs it one last time ..
Already changed it, originaly was WHILE TRUE.

I don't know, any help is appreciated ..
# 2  
Old 02-15-2010
Lose the functions.
Code:
trap 'echo "you hit control-c"; exit'  INT

# 3  
Old 02-16-2010
Using some posix-sh:
Code:
#!/bin/somesh

dothis()
{
   echo "you hit control-c"
   exit
}

settrap()
{
   trap 'dothis' INT
}

###########
settrap
while :
do
   echo "PID:$$"
   date
   sleep 10
done

Your own command can catch the signal, so test your trap using ex. date+sleep.
Do you get same result ?

Some shells not work correctly using function, but old sh style defination like funcname() works fine.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

--killing backround Procs spawned from the parent script with Ctrl+C trap

Hello: Am trying to understand why the method #2 works but method #1 does not. For both methods, sending CTRL+C should kill both the Parent script & all of the spanwd background procs. Method #1: ========================== #!/bin/sh ctrl_c() { echo "** Trapped CTRL-C" ... (3 Replies)
Discussion started by: gilgamesh
3 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. 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

4. 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

5. Shell Programming and Scripting

Help with getting a Ctrl-C trap working w/ a piped tail -f...

Hi All, Although each line below seems to work by itself, I've been having trouble getting the Control-C trap working when I add the "|perl -pe..." to the end of the tail -f line, below. (That |perl -pe statement basically just adds color to highlight the word "ERROR" while tailing a log... (2 Replies)
Discussion started by: chatguy
2 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

Trap CTRL-C and background process

Hello, I have a script which copies via scp several large files to a remote server. What I want is that even if someone hits CTRL-C, the scp commands continues till the end. Here is what I wrote #! /bin/bash function testFunction { echo "COPY START" scp large.tar.gz... (11 Replies)
Discussion started by: RobertFord
11 Replies

8. UNIX for Advanced & Expert Users

trap ctrl c in shell script

how to trap the ctrl c in unix shell script my script is running in while loop it should not be terminate with ctrl c. if i press ctrl c while running script it shloud ignore the same. please healp.......... thanks in advance (2 Replies)
Discussion started by: arvindng
2 Replies

9. 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

10. UNIX for Dummies Questions & Answers

ctrl-z problem in glance

in hpux, when i in glance, i cant press ctrl-z, what could be wrong? thanks (11 Replies)
Discussion started by: yls177
11 Replies
Login or Register to Ask a Question