Challenge - 5 Problems
Bash Script Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2: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"
Attempts:
2 left
💡 Hint
Look at the condition inside the function and the argument passed.
✗ Incorrect
The function checks if the input argument is greater than 10. Since 15 is greater than 10, it prints 'Greater'.
🧠 Conceptual
intermediate1:30remaining
Purpose of exit codes in bash script testing
Why are exit codes important when testing bash scripts?
Attempts:
2 left
💡 Hint
Think about how scripts communicate results to other programs or tests.
✗ Incorrect
Exit codes are numbers returned by commands or scripts to indicate success (usually 0) or failure (non-zero). Test scripts use these codes to decide if a test passed or failed.
🔧 Debug
advanced2: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"
Attempts:
2 left
💡 Hint
Check if the script handles file path arguments correctly.
✗ Incorrect
The script correctly checks if the file exists using -f and prints the appropriate message. Quotes around $1 are recommended but not mandatory here.
🚀 Application
advanced2: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?
Attempts:
2 left
💡 Hint
Use grep with exact match anchors and quiet mode to check output.
✗ Incorrect
Option D uses grep with ^ and $ anchors to match the exact output and -q to suppress output, making it suitable for testing exact output.
🧠 Conceptual
expert3:00remaining
Best practice for isolating tests in bash scripts
Which approach best isolates tests to avoid side effects when running multiple bash script tests?
Attempts:
2 left
💡 Hint
Think about how to keep tests independent and avoid interference.
✗ Incorrect
Running tests in subshells or temporary environments prevents changes in one test from affecting others, ensuring reliable and repeatable results.