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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to handle CTRL+Z or CTRL+C in shells script?
# 1  
Old 06-01-2012
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 remove those files from the folder. Can we include this in the same script itself.

I dont want to add trap "" 2 20. Because at some point of time, i have to use ctrl+z or ctrl+c, so i need to clear the files after breaking the script.

Thanks in advance.
Chelladurai.
# 2  
Old 06-01-2012
^Z doesn't "break" a script. It backgrounds it, and you can use fg to bring it back to the foreground.

I would use trap to handle the ^C press. It's not clear why you want to avoid it.
# 3  
Old 06-01-2012
Quote:
Originally Posted by ckchelladurai
Hi,
............

I dont want to add trap "" 2 20. Because at some point of time, i have to use ctrl+z or ctrl+c, so i need to clear the files after breaking the script.

Thanks in advance.
Chelladurai.
try add to these to beginning of the your script and then use the "CTRL-L" for remove files and exit from script
Code:
stty quit "^L"
trap "rm -f /yourfolder/* && kill -2 $$ " 3
......

regards
ygemici
# 4  
Old 06-01-2012
Hi,
Thanks for you reply.
Because, if user kickoff the script, and then they want to stop means, they have to user ctrl+c or ctrl+z, thats y I avoided that part.

For Ex:
I executed the script, it will create 4 of 5 files, in the mean time I have stopped the script, while stopping the script, i should handle those unwanted files.

Thanks,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

3. Shell Programming and Scripting

ctrl-c in shell script

The below script has Perl script in it where it gives the usage details. filer.sh echo echo -en "Enter filer : " read filer echo "test.pl -f $filer -F L" Output ========= The following hosts are online and available: Name Total Allocated Used ... (2 Replies)
Discussion started by: nareshkumar522
2 Replies

4. Shell Programming and Scripting

ctrl-c in script

how to send ctrl-c signal in scripts ??? i am executing top command in .exp script top command gives cpu running activities on your machine eg # top top - 18:41:01 up 8:38, 5 users, load average: 0.03, 0.16, 0.16 Tasks: 172 total, 1 running, 170 sleeping, 0 stopped, 1 zombie... (3 Replies)
Discussion started by: alexzander18
3 Replies

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

6. Shell Programming and Scripting

terminating script with CTRL+D

Hi, I'm trying to make a script that reads the console input and terminates with CTRL+D. It's absolutely basic but I don't know how to "read" the CTRL+D. I've tried a bunch of things like EOT=^D while //with & without quotations do read input echo $input done while while ] ... (12 Replies)
Discussion started by: sanchopansa
12 Replies

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

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

9. Shell Programming and Scripting

How to secure my script from Ctrl-C

Hi all I am looking for a way to ensure that once a user is logged in and running a script, he cannot break out of it. Thanks J (12 Replies)
Discussion started by: jhansrod
12 Replies

10. UNIX for Dummies Questions & Answers

Script ctrl+c

How do you make a script so the user can not press ctrl+c and break out of the script? Thanks, :) (1 Reply)
Discussion started by: newtounix
1 Replies
Login or Register to Ask a Question