if-elif-else in Bash Scripting - Time & Space Complexity
Let's see how the time it takes to run an if-elif-else decision changes as input changes.
We want to know how many steps the script takes when checking conditions.
Analyze the time complexity of the following code snippet.
read -p "Enter a number: " num
if [ "$num" -eq 1 ]; then
echo "Number is one"
elif [ "$num" -eq 2 ]; then
echo "Number is two"
else
echo "Number is something else"
fi
This script checks a number and prints a message based on its value.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking conditions one by one.
- How many times: At most, it checks each condition once in order.
Each input number causes the script to check conditions until one matches or all are checked.
| Input Size (n) | Approx. Operations |
|---|---|
| 1 | 1 check (first condition matches) |
| 2 | 2 checks (first fails, second matches) |
| 3 | 2 checks (both conditions fail, else runs) |
Pattern observation: The number of checks grows linearly with how far down the conditions we go.
Time Complexity: O(n)
This means the time grows linearly with the number of conditions checked.
[X] Wrong: "If statements always run in constant time no matter how many conditions there are."
[OK] Correct: The script checks conditions one by one, so more conditions mean more checks and more time.
Understanding how condition checks add up helps you explain script efficiency clearly and confidently.
"What if we replaced the if-elif-else with a case statement? How would the time complexity change?"
Practice
if-elif-else structure do in a Bash script?Solution
Step 1: Understand the purpose of if-elif-else
The if-elif-else structure checks conditions one by one and runs the matching block.Step 2: Compare with other options
Loops repeat commands, functions reuse code, comments disable code lines. Only if-elif-else chooses actions.Final Answer:
It lets the script choose between multiple actions based on conditions. -> Option DQuick Check:
if-elif-else = choose actions [OK]
- Confusing if-elif-else with loops
- Thinking it defines functions
- Mixing it up with comments
Solution
Step 1: Recall Bash if syntax
Bash requires square brackets around conditions and 'then' after the condition, with semicolons or newlines.Step 2: Check each option
if [ condition ]; then uses correct syntax: if [ condition ]; then. Others use wrong brackets or missing keywords.Final Answer:
if [ condition ]; then -> Option AQuick Check:
Correct if syntax = if [ condition ]; then [OK]
- Omitting brackets around condition
- Using parentheses instead of brackets
- Missing 'then' keyword
#!/bin/bash num=7 if [ $num -lt 5 ]; then echo "Less than 5" elif [ $num -eq 7 ]; then echo "Equal to 7" else echo "Other" fi
Solution
Step 1: Check the value of num and conditions
num=7. First condition checks if num < 5 (false). Second checks if num == 7 (true).Step 2: Determine which block runs
Since second condition is true, it prints "Equal to 7" and skips else.Final Answer:
Equal to 7 -> Option BQuick Check:
num=7 matches elif condition [OK]
- Assuming first condition runs
- Confusing -lt and -eq operators
- Missing fi causes syntax error
if [ $value -gt 10 ] echo "Greater than 10" elif [ $value -eq 10 ]; then echo "Equal to 10" else echo "Less than 10" fi
Solution
Step 1: Check syntax after if condition
In Bash, 'then' must follow the if condition, either on same line after semicolon or next line.Step 2: Verify other parts
elif has 'then', fi is present, operators are correct. Only missing 'then' after if.Final Answer:
Missing 'then' after first if condition -> Option CQuick Check:
if needs 'then' keyword [OK]
- Forgetting 'then' after if
- Misplacing semicolons
- Confusing elif syntax
num. Which script correctly uses if-elif-else to do this?Solution
Step 1: Check comparison operators
Bash uses -gt, -eq, -lt for numeric comparisons. #!/bin/bash if [ $num -gt 0 ]; then echo "Positive" elif [ $num -eq 0 ]; then echo "Zero" else echo "Negative" fi uses correct operators.Step 2: Verify logic and syntax
#!/bin/bash if [ $num -gt 0 ]; then echo "Positive" elif [ $num -eq 0 ]; then echo "Zero" else echo "Negative" fi correctly checks >0, ==0, else negative. Others use wrong operators or syntax.Final Answer:
A -> Option AQuick Check:
Use -gt, -eq for numbers in if-elif-else [OK]
- Using > or == inside [ ]
- Missing then keyword
- Wrong operator for numeric comparison
Ready to prove it?
Pass the test and this pattern moves unknown
