0
0
Bash Scriptingscripting~20 mins

Script testing strategies in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Bash Script Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of a simple test function in bash
What is the output of this bash script when run in a terminal?
Bash Scripting
#!/bin/bash

test_function() {
  if [ "$1" -gt 10 ]; then
    echo "Greater"
  else
    echo "Smaller or equal"
  fi
}

result=$(test_function 15)
echo "$result"
ASmaller or equal
BGreater
C15
DSyntax error
Attempts:
2 left
💡 Hint
Look at the condition inside the function and the argument passed.
🧠 Conceptual
intermediate
1:30remaining
Purpose of exit codes in bash script testing
Why are exit codes important when testing bash scripts?
AThey change the script's output text automatically
BThey prevent the script from running if errors occur
CThey indicate success or failure of commands, helping automate test checks
DThey format the script's output for readability
Attempts:
2 left
💡 Hint
Think about how scripts communicate results to other programs or tests.
🔧 Debug
advanced
2:00remaining
Identify the error in this test script snippet
What error will this bash script produce when run?
Bash Scripting
#!/bin/bash

check_file() {
  if [ -f $1 ]; then
    echo "File exists"
  else
    echo "File missing"
  fi
}

check_file "/tmp/testfile"
ANo error, outputs 'File exists' or 'File missing'
BSyntax error due to missing quotes around $1 in the if condition
CRuntime error: command not found
DOutputs nothing
Attempts:
2 left
💡 Hint
Check if the script handles file path arguments correctly.
🚀 Application
advanced
2:30remaining
Automate testing of a script output
You want to test if your script 'hello.sh' outputs exactly 'Hello World!'. Which command correctly tests this and returns exit code 0 if it matches?
Aif ./hello.sh | grep -q 'Hello World'; then echo 'Test failed'; else echo 'Test passed'; fi
Bif ./hello.sh | grep 'Hello'; then echo 'Test passed'; else echo 'Test failed'; fi
Cif ./hello.sh | grep -v 'Hello World!'; then echo 'Test passed'; else echo 'Test failed'; fi
Dif ./hello.sh | grep -q '^Hello World!$'; then echo 'Test passed'; else echo 'Test failed'; fi
Attempts:
2 left
💡 Hint
Use grep with exact match anchors and quiet mode to check output.
🧠 Conceptual
expert
3:00remaining
Best practice for isolating tests in bash scripts
Which approach best isolates tests to avoid side effects when running multiple bash script tests?
ARun each test in a separate subshell or temporary environment
BRun all tests sequentially in the same shell without resetting variables
CUse global variables to share state between tests
DRun tests only once to avoid repeated side effects
Attempts:
2 left
💡 Hint
Think about how to keep tests independent and avoid interference.