0
0
Bash Scriptingscripting~10 mins

Function libraries (sourcing scripts) 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 source the functions from the file lib.sh.

Bash Scripting
source [1]
Drag options to blanks, or click blank then click option'
A./lib.sh
Blib
Cfunctions.sh
Dlib.sh
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to add ./ before the script name.
Using just the script name without path.
Using a wrong filename.
2fill in blank
medium

Complete 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'
Autils.sh
B./utils.sh
Cutils
Dfunctions.sh
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.
3fill in blank
hard

Fix 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'
Ainclude
Bimport
Csource
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Using include or import which are not bash commands.
Trying to run the script as a separate process.
4fill in blank
hard

Fill 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'
A-f
B-r
C-d
D-x
Attempts:
3 left
💡 Hint
Common Mistakes
Using -d which checks for directories.
Using -x which checks for executable permission.
5fill in blank
hard

Fill 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'
A$1
Dlib.sh
Attempts:
3 left
💡 Hint
Common Mistakes
Hardcoding the filename instead of using the argument.
Using different variables for each blank.