How to do it...

Let's start our activity as follows:

  1. Open a new terminal and create a new script by the name of mylock.sh with the following contents:
#!/bin.bashLOCKFILE="/tmp/mylock"function setup() {   # $$ will provide the process ID  TMPFILE="${LOCKFILE}.$$"  echo "$$" > "${TMPFILE}"    # Now we use hard links for atomic file operations   if ln "${TMPFILE}" "${LOCKFILE}" 2>&- ; then      echo "Created tmp lock"  else      echo "Locked by" $(<$LOCKFILE)      rm "${TMPFILE}"      exit 1  fi  trap "rm ${TMPFILE} ${LOCKFILE}" SIGINT SIGTERM SIGKILL}setupecho "Door was left unlocked"exit 0
  1. Execute the script with the $ bash mylock.sh script and review the console's output.
  2. Next, we know that the script is looking for a particular lock file. What happens when we create ...

Get Bash Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.