Alexander D
Alexander D

Reputation: 101

Could someone help explain what this Bash script is doing?

I am new to linux and although I am familiar with the concept of a lockfile in scripts, I came across that code in another script and thought it was pretty cool, albeit I have no idea how it works. Would anyone be kind enough to go through exactly what the cleanup file and finalize function is doing?

As opposed to a if else statement for a lockfile

cleanup_file="$(mktemp -q)"
finalize()
{ 
  set +e
  if test -f "$cleanup_file"
  then
    while read f
    do
      unlink "$f"
    done < "$cleanup_file"
    unlink "$cleanup_file"
  fi
}

trap 'finalize' HUP INT QUIT TERM EXIT

Upvotes: 1

Views: 57

Answers (1)

codeforester
codeforester

Reputation: 43079

The finalize function isn't dealing with any locks. It just removes all files whose names are in the cleanup file. It also removes the cleanup file.

It appears that the larger script appends names of temp files to the cleanup file and the trap signal handler takes care of cleaning them up upon termination of the script.

Upvotes: 2

Related Questions