How to continue in the loop after trapping signal


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to continue in the loop after trapping signal
# 1  
Old 05-02-2010
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!!)..
Code:
$ cat sigtrap
#!/usr/bin/perl
$SIG{'INT'} = 'ABORT';
    sub ABORT {
    print "\nStop the loop?? (y/n) : ";
        chop($ch=<STDIN>); 
        
 if ($ch =~ /[yY]/)
    {      
     
          
    exit(1); 

     }
  else 
   { 
     redo;
   } 
    }
$no=0;
while (1)
 {
   print "$no\n";
   sleep 2;
   $no++
 }

Code:
$ ./sigtrap 
0
1
2
3
^C
Stop the loop?? (y/n) : n
Can't "redo" outside a loop block at ./sigtrap line 16, <STDIN> line 1.

by using 'trap' in bash...we can continue in loop....if we write 'trap' in the loop...

but how can i solve this in perl!! plz help!!


thanx.
# 2  
Old 05-02-2010
When I removed the "else" part from you script it seems to work:
Code:
$ perl sigtra.pl
0
1
2
3
^C
Stop the loop?? (y/n) : n
4
5
^C
Stop the loop?? (y/n) : n
6
7
^C
Stop the loop?? (y/n) : y
$

It that what you tried to achieve?
# 3  
Old 05-04-2010
thanx pseudocoder!! it worked....
but plz tell me the reason behind this....i cud'nt get why removiing 'else' part make program work!!
# 4  
Old 05-04-2010
You're welcome.
I assume it's because "redo" is not the proper command to jump back in the while loop.
Unfortunately I don't know a proper command which would do that.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Continue the loop if the value is not found

Dear Help, Is it possible to continue the loop by going to the next available value, if the 'expected value' is not found. I have a list of values which might not get incremented by fixed value and hence the loop could break and the script could terminate. Any suggestion is appreciated. ... (1 Reply)
Discussion started by: Indra2011
1 Replies

2. Shell Programming and Scripting

Continue Processing after a signal is caught

Is it possible to continue after signal is caught and control goes to function specified in the trap statement? (3 Replies)
Discussion started by: Soham
3 Replies

3. Shell Programming and Scripting

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 #trap trap_control 2 #while true #do... (6 Replies)
Discussion started by: gangsta
6 Replies

4. Shell Programming and Scripting

Error Trapping

Hi, I have one shell script as below while read SegList do if test -s ${SourceFile_Path}/${Segment_List_Temp} then ls -r -1 ${FTP_Path}/${SegList}.DAT.${Datelist}.GZ|cut -d '.' -f2>>${SourceFile_Path}/${List_Temp} echo "IF above statment Fail I want to Create Emtpy File How to Trapp... (3 Replies)
Discussion started by: samadhanpatil
3 Replies

5. Shell Programming and Scripting

trapping error for a grep in for a loop

How can I trap and print "cannot find the pattern" when the grep is unable to find the specified pattern in the file using the for loop below ? Any help would be appreciated. bash3.4> cat test_file apple orange pineapple blackberry script: for x in `grep -n "mango" test_file... (4 Replies)
Discussion started by: jville
4 Replies

6. Shell Programming and Scripting

Trapping Enter command !!

Hi I have written a script which works well .. It's divided into a series of jobs one running after another But on pressing the enter key the whole script goes haywire .. I want a way to trap this enter command and echo it as invalid input .. Any suggestions highly appreciated... Thanks :) (2 Replies)
Discussion started by: ultimatix
2 Replies

7. UNIX for Dummies Questions & Answers

trapping errors

I am using unixs script to submit programs (SQRS) and need to trap any time of error that is received once the job finishes. Examples of the type of errors I am getting Error! SQR Failed To Process mkdir: Failed to make directory These are showing up in a log file but I do not want to... (3 Replies)
Discussion started by: TimHortons
3 Replies

8. Shell Programming and Scripting

Trapping $! output

hey all, I have a script that creates and then distributes html files via scp on a 60 second cycle. On occasions the scp will hang and not complete with the cycle. When running the scp command in the back ground it returns the PID. How do I trap $! in the script? job=`echo $!` returns... (2 Replies)
Discussion started by: nhatch
2 Replies

9. Shell Programming and Scripting

Error Trapping

Hi, Can anybody tell me how to error trap an empty line. If i am asked for a password and I hit enter without entering any text, how do i display an error? Thanks Kev (6 Replies)
Discussion started by: kev112
6 Replies

10. UNIX for Dummies Questions & Answers

trapping keys

how do i trap enter command entered by a user. actually i am throwing a screen this screen has no input but this screen should be displayes unless and until the user presses the enter key. as the user presses enter key the command prompt should come. how do i achieve this (1 Reply)
Discussion started by: sunil bajaj
1 Replies
Login or Register to Ask a Question