Script testing strategies in Bash Scripting - Time & Space Complexity
Testing a script often involves running it multiple times with different inputs. Understanding how the time to test grows as you add more tests helps plan your work.
We want to know: How does the testing effort increase when we add more test cases?
Analyze the time complexity of the following bash script testing loop.
#!/bin/bash
inputs=("input1" "input2" "input3" "input4")
for input in "${inputs[@]}"; do
./my_script.sh "$input"
done
This script runs another script multiple times, once for each input in the list.
Look at what repeats in the script.
- Primary operation: Running
./my_script.shfor each input. - How many times: Once per input in the
inputsarray.
Each new input adds one more run of the script.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 script runs |
| 100 | 100 script runs |
| 1000 | 1000 script runs |
Pattern observation: The total testing time grows directly with the number of inputs.
Time Complexity: O(n)
This means if you double the number of test inputs, the total testing time roughly doubles.
[X] Wrong: "Running tests in a loop is instant no matter how many inputs there are."
[OK] Correct: Each test run takes time, so more inputs mean more total time spent testing.
Knowing how testing time grows helps you plan and explain your testing approach clearly. It shows you understand how your work scales as projects grow.
"What if we ran tests in parallel instead of one after another? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of testing scripts
Testing helps catch mistakes before the script runs on important data or systems.Step 2: Identify the benefit of early error detection
Finding errors early prevents failures and saves time and effort in fixing issues later.Final Answer:
To find and fix errors early, avoiding bigger problems later -> Option DQuick Check:
Testing = Catch errors early [OK]
- Thinking testing makes scripts faster automatically
- Confusing testing with rewriting code
- Believing testing reduces file size
Solution
Step 1: Recall bash debugging options
The-xoption runs the script showing each command before execution.Step 2: Differentiate from other options
-vprints shell input lines,-eexits on error,-cruns commands from string.Final Answer:
bash -x script.sh -> Option CQuick Check:
Debug with -x = bash -x [OK]
- Confusing -v with -x for debugging
- Using -e expecting debug output
- Trying -c without command string
bash -x test.sh? #!/bin/bash x=5 if [ $x -gt 3 ]; then echo "Greater" else echo "Smaller" fi
Solution
Step 1: Understand bash -x output
Running with-xprints each command with a plus sign before executing it.Step 2: Trace the script commands
Commandsx=5, the test[ $x -gt 3 ], andecho "Greater"are printed before output.Final Answer:
+ x=5 + '[' 5 -gt 3 ']' + echo 'Greater' Greater -> Option AQuick Check:
bash -x shows commands with + [OK]
- Ignoring the debug lines starting with +
- Choosing output without debug info
- Confusing else branch output
#!/bin/bash filename="myfile.txt" if [ -f $filename ] then echo "File exists" else echo "File missing" fi
Solution
Step 1: Identify variable usage in test condition
The variable$filenameis unquoted, which can cause errors if it contains spaces.Step 2: Understand quoting importance in bash tests
Quoting variables prevents word splitting and errors in test expressions.Final Answer:
Missing quotes around $filename can cause errors if filename has spaces -> Option BQuick Check:
Quote variables in tests = Missing quotes around $filename can cause errors if filename has spaces [OK]
- Thinking [[ ]] is required instead of [ ]
- Confusing -f and -e options
- Believing echo needs braces
Solution
Step 1: Understand exit codes in bash scripts
Scripts use exit codes to signal success (0) or failure (non-zero).Step 2: Automate testing by running script with inputs and checking
Running script with inputs and verifying$?$?confirms correct behavior automatically.Final Answer:
Write test cases that run the script with different inputs and check the exit code using $? -> Option AQuick Check:
Test exit codes with $? = Write test cases that run the script with different inputs and check the exit code using$?[OK]
- Relying only on manual output checks
- Ignoring exit codes in tests
- Assuming renaming files triggers tests
Ready to prove it?
Pass the test and this pattern moves unknown
