0
0
Bash Scriptingscripting~10 mins

Script testing strategies 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 run a script and check its exit status.

Bash Scripting
bash [1]; echo $?  # Prints the exit status of the script
Drag options to blanks, or click blank then click option'
Amyscript.sh
Brun.sh
Cscript.py
Dtest.sh
Attempts:
3 left
💡 Hint
Common Mistakes
Using a Python script name instead of a bash script.
Forgetting to specify the script file name.
2fill in blank
medium

Complete the code to test if a variable is empty in a bash script.

Bash Scripting
if [ -z [1] ]; then echo "Empty"; fi
Drag options to blanks, or click blank then click option'
A$var
Bvar
C"var"
D$var.txt
Attempts:
3 left
💡 Hint
Common Mistakes
Not using the $ sign before the variable name.
Using quotes incorrectly around the variable.
3fill in blank
hard

Fix the error in the code to correctly test if a file exists.

Bash Scripting
if [[ -f [1] ]]; then echo "File exists"; fi
Drag options to blanks, or click blank then click option'
Afilename.txt
Bfilename
C"filename"
D$filename
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the $ sign before the variable.
Using single brackets [ ] with variable without quotes.
4fill in blank
hard

Fill both blanks to create a loop that tests each file in a directory.

Bash Scripting
for file in [1]; do if [[ -f [2] ]]; then echo "$file exists"; fi; done
Drag options to blanks, or click blank then click option'
A"*"
B$file
Cfile
D"file"
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting the '*' wildcard.
Using 'file' without $ to reference the variable.
5fill in blank
hard

Fill all three blanks to create a function that tests if input is a directory and prints a message.

Bash Scripting
check_dir() {
  if [[ -d [1] ]]; then
    echo "[2] is a directory"
  else
    echo "Not a directory"
  fi
}
Drag options to blanks, or click blank then click option'
A$1
B"$1"
Dinput
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting the variable, causing errors with spaces.
Using variable names instead of positional parameters.