1

In my shell script, im deleting a file at the end of script. And i need it to be deleted even if the script was stopped by (ctrl c or ctrl z)..Is there any way to read that and delete the file?

Thanks in advance

2 Answers 2

3

Like @pgl said, trap is what you want. The syntax is:

trap <actionhere> <event> [event...]

The action is one and only one argument, but it can run several commands. The event is either exit (when you call exit manually) or a signal by its "short" name, ie without the SIG prefix (for instance, INT for SIGINT.

Example:

trap "rm -f myfile" INT exit

You can change the trap all along the script. And of course, you can use variable interpolation in your action.

Sign up to request clarification or add additional context in comments.

1 Comment

I want to know more about shell's event programming syntax, example codes.
2

You can catch ctrl+c's with the trap builtin. Try this to get started:

help trap

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.