I've a bash script with some file manipulations and I would like to process a loop until the end of the block after pressing CTRL+C. I've made an example:
#!/bin/bash
# Register signal handler
ABORT=0;
trap ABORT=1 SIGINT;
# Create temp dir
TEMPDIR=$(mktemp -d -t $0);
# Helper functions
function do_other_stuff {
true;
}
# Process files
for ((COUNTER = 0; COUNTER < 3 && ABORT == 0; COUNTER++)); do
FILE=/some/directory/$COUNTER.txt;
BASE=$(basename $FILE);
cp $FILE $TEMPDIR;
> $FILE;
do_other_stuff;
cp $TEMPDIR/$BASE $FILE;
rm $TEMPDIR/$BASE;
done;
rm -rf $TEMPDIR;
This seems to work quite well, but I noticed, that sometimes BASE in the statement
BASE=$(basename $FILE);
is not set, if the trap happens to occur during the basename command. This leads to errors in the cp
and following commands.
Did I miss something there? How is the intention of bash to recover from traps? Is there another solution with the same effect?