Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the variable name 'lockfile' without the '$' sign.
Checking a wrong file path.
✗ Incorrect
The script checks if the lock file path stored in the variable 'lockfile' exists. The correct path is '/tmp/mylockfile.lock'.
2fill in blank
mediumComplete 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'
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'.
✗ Incorrect
To create the lock file, use the variable 'lockfile' with a '$' to get its value.
3fill in blank
hardFix 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'
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.
✗ Incorrect
To remove the lock file, use '$lockfile' to refer to the variable's value.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'lockfile' without '$' in the test condition.
Using 'rm' instead of 'touch' to create the file.
✗ Incorrect
Check if the file at '$lockfile' exists, then create it with 'touch $lockfile'.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names without quotes in the dictionary.
Checking the wrong key in the dictionary.
✗ Incorrect
The dictionary 'locks' maps script names to lock file paths. Use 'myscript' as key and its lock file path. Then check if the lock file for the current script exists.