0
0
Bash Scriptingscripting~10 mins

Lock files for single instance in Bash Scripting - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a lock file before running the script.

Bash Scripting
lockfile=/tmp/mylockfile.lock
if [ -e [1] ]; then
  echo "Script is already running."
  exit 1
fi
Drag options to blanks, or click blank then click option'
A/tmp/mylockfile.lock
Blockfile
C/var/lock/myscript.lock
D/tmp/lockfile
Attempts:
3 left
💡 Hint
Common Mistakes
Using the variable name 'lockfile' without the '$' sign.
Checking a wrong file path.
2fill in blank
medium

Complete the code to create the lock file after confirming it does not exist.

Bash Scripting
touch [1]
Drag options to blanks, or click blank then click option'
A/var/lock/myscript.lock
B$lockfile
C/tmp/mylockfile.lock
Dlockfile
Attempts:
3 left
💡 Hint
Common Mistakes
Using the variable name without '$' so the file named 'lockfile' is created.
Using a different file path than the one stored in 'lockfile'.
3fill in blank
hard

Fix the error in the code to remove the lock file after the script finishes.

Bash Scripting
rm [1]
Drag options to blanks, or click blank then click option'
Alockfile
B/tmp/mylockfile.lock
C$lockfile
D/var/lock/myscript.lock
Attempts:
3 left
💡 Hint
Common Mistakes
Removing a file named literally 'lockfile' instead of the path stored in the variable.
Removing the wrong file path.
4fill in blank
hard

Fill both blanks to create a safe lock file check and creation.

Bash Scripting
if [ -e [1] ]; then
  echo "Already running"
  exit 1
fi
[2] [1]
Drag options to blanks, or click blank then click option'
A$lockfile
Blockfile
Ctouch
Drm
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'lockfile' without '$' in the test condition.
Using 'rm' instead of 'touch' to create the file.
5fill in blank
hard

Fill all three blanks to create a dictionary of running scripts with their lock files and check if a script is running.

Bash Scripting
declare -A locks=([[1]]=[2])
script="myscript"
if [ -e "${locks[$ [3] ]}" ]; then
  echo "$script is already running."
  exit 1
fi
Drag options to blanks, or click blank then click option'
A"myscript"
B"/tmp/myscript.lock"
Cscript
D"myotherscript"
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names without quotes in the dictionary.
Checking the wrong key in the dictionary.