help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] too paranoid?


From: Bob Proulx
Subject: Re: [Help-bash] too paranoid?
Date: Thu, 10 Mar 2016 17:48:37 -0700
User-agent: Mutt/1.5.24 (2015-08-30)

John McKown wrote:
> I'm considering how to make sure that all the "temporary" files that I use
> in a script are deleted when the script ends. I'm on Linux and don't really
> care about cross platform compatibility. I.e. I don't care if it doesn't
> work on Windows. I was thinking of doing something like:

In addition to the other suggestions, which were good, let me suggest
just using normal scripting to create temporary files and clean them
up when the script ends.  A few simple examples follow.

Bob

================================================================
Example using a temporary file that is automatically cleaned up when
the script program ends normally or is interrupted and ends.

#!/bin/sh

# ...other stuff in the shell skeleton file here...

unset tmpfile
cleanup() {
  test -n "$tmpfile" && rm -f "$tmpfile"
}
trap "cleanup" EXIT
trap "cleanup; trap - HUP; kill -HUP $$" HUP
trap "cleanup; trap - INT; kill -INT $$" INT
trap "cleanup; trap - QUIT; kill -QUIT $$" QUIT
trap "cleanup; trap - TERM; kill -TERM $$" TERM

tmpfile=$(mktemp) || exit 1

echo "...something..." > "$tmpfile"
cat "$tmpfile"

exit 0

================================================================
Example using multiple temporary files that are automatically cleaned
up when the script program ends normally or is interrupted and ends.

#!/bin/sh

# ...other stuff in the shell skeleton file here...

unset tmpfile1
unset tmpfile2
unset tmpfile3
cleanup() {
  test -n "$tmpfile1" && rm -f "$tmpfile1"
  test -n "$tmpfile2" && rm -f "$tmpfile2"
  test -n "$tmpfile3" && rm -f "$tmpfile3"
}
trap "cleanup" EXIT
trap "cleanup; trap - HUP; kill -HUP $$" HUP
trap "cleanup; trap - INT; kill -INT $$" INT
trap "cleanup; trap - QUIT; kill -QUIT $$" QUIT
trap "cleanup; trap - TERM; kill -TERM $$" TERM

tmpfile1=$(mktemp) || exit 1
tmpfile2=$(mktemp) || exit 1
tmpfile3=$(mktemp) || exit 1

echo "...something 1..." > "$tmpfile1"
cp "$tmpfile1" "$tmpfile2"
cp "$tmpfile2" "$tmpfile3"
cat "$tmpfile3"

exit 0

================================================================
Example using a temporary file where all output is emailed to the
caller.  Such as when called from cron or a daemon or any place where
output is far away from the calling of the script.

#!/bin/sh

# ...other stuff in the shell skeleton file here...

unset tmpfile
cleanup() {
  test -n "$tmpfile" && rm -f "$tmpfile"
}
trap "cleanup" EXIT
trap "cleanup; trap - HUP; kill -HUP $$" HUP
trap "cleanup; trap - INT; kill -INT $$" INT
trap "cleanup; trap - QUIT; kill -QUIT $$" QUIT
trap "cleanup; trap - TERM; kill -TERM $$" TERM

tmpfile=$(mktemp) || exit 1

exec </dev/null >"$tmpfile" 2>&1

echo "...do stuff here that may make output..."
echo "...do stuff here that may make output..."
echo "...do stuff here that may make output..."

exec >/dev/null 2>&1            # close previous output file

if [ -s "$tmpfile" ]; then
  # There was output.  Mail it.
  mailx -s "output from doing stuff" "$(whoami)" < "$tmpfile"
fi

exit 0



reply via email to

[Prev in Thread] Current Thread [Next in Thread]