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
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Like @pgl said,
trapis 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 callexitmanually) or a signal by its “short” name, ie without theSIGprefix (for instance,INTforSIGINT.Example:
trap "rm -f myfile" INT exitYou can change the trap all along the script. And of course, you can use variable interpolation in your action.