Challenge - 5 Problems
If-Elif-Else Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
Output of a simple if-elif-else script
What is the output of this Bash script when run with argument 15?
Bash Scripting
#!/bin/bash num=$1 if [ "$num" -lt 10 ]; then echo "Less than 10" elif [ "$num" -lt 20 ]; then echo "Between 10 and 19" else echo "20 or more" fi
Attempts:
2 left
💡 Hint
Check the numeric comparison operators and the order of conditions.
✗ Incorrect
The script checks if the number is less than 10, then less than 20. Since 15 is not less than 10 but less than 20, it prints 'Between 10 and 19'.
🔧 Debug
intermediate2:00remaining
Identify the error in this if-elif-else script
What error will this Bash script produce when run?
Bash Scripting
#!/bin/bash num=5 if [ $num < 10 ]; then echo "Less than 10" elif [ $num < 20 ]; then echo "Between 10 and 19" else echo "20 or more" fi
Attempts:
2 left
💡 Hint
Check the comparison operators used inside the test brackets.
✗ Incorrect
In Bash, '<' is a redirection operator, not a numeric comparison. The correct operator is '-lt'. Using '<' causes a syntax error.
🚀 Application
advanced2:30remaining
Script to categorize a number with if-elif-else
Which script correctly categorizes a number into 'small' (less than 5), 'medium' (5 to 10), or 'large' (greater than 10)?
Attempts:
2 left
💡 Hint
Check the numeric ranges carefully and the operators used.
✗ Incorrect
Option A correctly uses '-lt 5' for small, '-le 10' for medium (including 5 to 10), and else for large. Others misclassify or overlap ranges.
🧠 Conceptual
advanced1:30remaining
Understanding else placement in if-elif-else
What happens if you omit the else block in an if-elif-else chain in Bash?
Attempts:
2 left
💡 Hint
Think about what else does in a conditional chain.
✗ Incorrect
The else block is optional. If omitted and no conditions match, the script simply skips the blocks and continues without error.
💻 Command Output
expert2:30remaining
Output of nested if-elif-else with multiple conditions
What is the output of this script when run with argument 0?
Bash Scripting
#!/bin/bash num=$1 if [ "$num" -gt 0 ]; then if [ "$num" -lt 10 ]; then echo "Positive single digit" else echo "Positive double digit or more" fi elif [ "$num" -eq 0 ]; then echo "Zero" else echo "Negative number" fi
Attempts:
2 left
💡 Hint
Trace the conditions carefully for input zero.
✗ Incorrect
The script first checks if number is greater than zero (false for 0), then checks if number equals zero (true), so it prints 'Zero'.