if-then-fi structure in Bash Scripting - Time & Space Complexity
We want to understand how the time a bash script takes changes when it uses an if-then-fi structure.
Specifically, we ask: How does the script's running time grow as input changes when using if-then-fi?
Analyze the time complexity of the following code snippet.
if [ "$1" -gt 10 ]; then
echo "Number is greater than 10"
else
echo "Number is 10 or less"
fi
This script checks if the first input number is greater than 10 and prints a message accordingly.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Single if condition check
- How many times: Exactly once per script run
The script checks the condition once, no matter the input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 check |
| 100 | 1 check |
| 1000 | 1 check |
Pattern observation: The number of operations stays the same regardless of input size.
Time Complexity: O(1)
This means the script runs in constant time, doing the same amount of work no matter the input.
[X] Wrong: "The if-then-fi structure takes longer if the input number is bigger."
[OK] Correct: The condition is checked once, so input size or value does not affect how many steps the script takes.
Understanding that simple condition checks run in constant time helps you explain script efficiency clearly and confidently.
"What if we added a loop inside the if block that runs n times? How would the time complexity change?"
Practice
if-then-fi structure in a bash script?Solution
Step 1: Understand the role of if-then-fi
The if-then-fi structure checks a condition and runs commands only if that condition is true.Step 2: Compare with other options
Repeating commands is done with loops, functions define reusable code, and variables store data, so these do not match if-then-fi.Final Answer:
To run commands only if a certain condition is true -> Option AQuick Check:
if-then-fi controls conditional execution = D [OK]
- Confusing if-then-fi with loops
- Thinking if-then-fi defines functions
- Using if-then-fi to declare variables
Solution
Step 1: Check the correct if syntax in bash
Bash requires conditions inside square brackets with spaces, then 'then', commands, and ending with 'fi'. if [ $x -gt 5 ]; then echo "Yes"; fi matches this.Step 2: Identify errors in other options
if ($x > 5) { echo "Yes" } uses wrong syntax with parentheses and braces (not bash). if $x > 5 then echo "Yes" fi misses semicolons and brackets. if $x -gt 5 echo "Yes" fi misses 'then' keyword.Final Answer:
if [ $x -gt 5 ]; then echo "Yes"; fi -> Option CQuick Check:
Correct bash if syntax = C [OK]
- Missing spaces inside [ ]
- Omitting 'then' keyword
- Using wrong brackets or symbols
#!/bin/bash x=10 if [ $x -lt 5 ]; then echo "Less" fi echo "Done"
Solution
Step 1: Evaluate the condition
The variable x is 10. The condition checks if 10 is less than 5, which is false.Step 2: Determine which commands run
Since the condition is false, the echo "Less" inside if does not run. The echo "Done" after if runs always.Final Answer:
Done -> Option DQuick Check:
Condition false, only "Done" prints = A [OK]
- Assuming if block runs even if condition false
- Confusing -lt with -gt
- Ignoring commands after fi
if [ $num -eq 5 ] then echo "Number is five" fi
Solution
Step 1: Check if syntax correctness
The condition is inside [ ] with spaces, '-eq' is correct for numeric equality, 'then' is on next line, and 'fi' closes the block.Step 2: Confirm no syntax errors
Newline after condition is valid instead of semicolon. All keywords are present and correctly placed.Final Answer:
No error, script is correct -> Option BQuick Check:
Proper if-then-fi syntax = B [OK]
- Thinking newline after condition is error
- Confusing -eq with other operators
- Forgetting fi keyword
n is even, and "Odd" otherwise. Which of these is the correct if-then-fi structure to do this?Solution
Step 1: Understand how to check evenness in bash
We use arithmetic expansion $(( )) to calculate remainder of n divided by 2. If remainder is 0, number is even.Step 2: Analyze each option's correctness
if [ $((n % 2)) -eq 0 ]; then echo "Even"; else echo "Odd"; fi correctly uses [ $((n % 2)) -eq 0 ] with then, else, fi. if [ $n % 2 == 0 ]; then echo "Even"; else echo "Odd"; fi uses invalid syntax inside [ ]. if (( n % 2 == 0 )) then echo "Even"; else echo "Odd"; fi misses semicolon before 'then' causing syntax error. if [ $n / 2 -eq 0 ]; then echo "Even"; else echo "Odd"; fi incorrectly uses division instead of modulo.Final Answer:
if [ $((n % 2)) -eq 0 ]; then echo "Even"; else echo "Odd"; fi -> Option AQuick Check:
Modulo with $(( )) inside [ ] and proper if-then-fi = A [OK]
- Using % inside [ ] without $(( ))
- Using == instead of -eq for numbers
- Confusing division with modulo
Ready to prove it?
Pass the test and this pattern moves unknown
