Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to source the functions from the file lib.sh.
Bash Scripting
source [1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to add
./ before the script name.Using just the script name without path.
Using a wrong filename.
✗ Incorrect
To source a script in the current directory, you use
source ./filename. This runs the script in the current shell, making its functions available.2fill in blank
mediumComplete the code to check if the file utils.sh exists before sourcing it.
Bash Scripting
if [ -f [1] ]; then source ./utils.sh fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for the wrong filename.
Omitting the path and checking a file that may not be found.
Using
-d instead of -f.✗ Incorrect
The
-f test checks if a file exists and is a regular file. Using ./utils.sh ensures the correct path is checked.3fill in blank
hardFix the error in the code to source helpers.sh safely.
Bash Scripting
if [ -f helpers.sh ]; then [1] ./helpers.sh fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
include or import which are not bash commands.Trying to run the script as a separate process.
✗ Incorrect
In bash,
source is the correct command to run a script in the current shell. include and import are not valid bash commands.4fill in blank
hardFill both blanks to source functions.sh only if it exists and is readable.
Bash Scripting
if [ [1] functions.sh -a [2] functions.sh ]; then source ./functions.sh fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
-d which checks for directories.Using
-x which checks for executable permission.✗ Incorrect
Use
-f to check if the file exists and is a regular file, and -r to check if it is readable.5fill in blank
hardFill all three blanks to create a function load_lib that sources a given script if it exists.
Bash Scripting
load_lib() {
if [ -f [1] ]; then
source ./[2]
else
echo "File [3] not found."
fi
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Hardcoding the filename instead of using the argument.
Using different variables for each blank.
✗ Incorrect
The function uses the first argument
$1 to check, source, and print the filename dynamically.