Re: Cleanup in a bash script
On Sat, Sep 28, 2024 at 10:13:59AM -0400, Greg Wooledge wrote:
> On Sat, Sep 28, 2024 at 14:53:10 +0100, Tim Woodall wrote:
> > Is there a way in bash to guarantee that a trap gets called for cleanup
> > in a script?
>
> #!/bin/bash
> trap cleanup EXIT
> cleanup() {
> ...
> }
>
> This works in bash -- i.e., it calls the cleanup function regardless
> of whether the shell exits by calling "exit", or by falling off the
> end of the script, or by receiving a fatal signal. It does NOT work in
> /bin/sh (dash, or any other implementation). You have been warned.
*ALL* scripts MUST start with a #! line as you have given above.
Anyone who does not do that deserves what they get.
--
Alain Williams
Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT Lecturer.
+44 (0) 787 668 0256 https://www.phcomp.co.uk/
Parliament Hill Computers. Registration Information: https://www.phcomp.co.uk/Contact.html
#include <std_disclaimer.h>
Reply to: