Challenge - 5 Problems
If-Then-Fi Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of this Bash script using if-then-fi?
Consider the following Bash script. What will it print when executed?
Bash Scripting
x=5 if [ $x -gt 3 ] then echo "Greater" fi
Attempts:
2 left
💡 Hint
Remember the condition checks if x is greater than 3.
✗ Incorrect
The variable x is 5, which is greater than 3, so the condition is true and the echo command runs.
💻 Command Output
intermediate2:00remaining
What happens if the condition in if-then-fi is false?
What will this Bash script print when executed?
Bash Scripting
x=2 if [ $x -gt 3 ] then echo "Greater" fi
Attempts:
2 left
💡 Hint
The condition checks if 2 is greater than 3.
✗ Incorrect
Since 2 is not greater than 3, the condition is false and the echo command inside the if block does not run.
📝 Syntax
advanced2:00remaining
Identify the syntax error in this if-then-fi structure
Which option correctly fixes the syntax error in this Bash script?
Bash Scripting
x=4 if [ $x -gt 3 ] echo "Greater" fi
Attempts:
2 left
💡 Hint
The if statement needs a 'then' keyword before commands.
✗ Incorrect
The 'then' keyword is required after the if condition to start the block of commands.
🚀 Application
advanced2:00remaining
What is the output of this nested if-then-fi script?
Analyze the script and choose the correct output.
Bash Scripting
x=7 y=10 if [ $x -gt 5 ] then if [ $y -lt 15 ] then echo "Both conditions met" fi fi
Attempts:
2 left
💡 Hint
Both x and y satisfy their conditions.
✗ Incorrect
x is 7 which is greater than 5, and y is 10 which is less than 15, so both if conditions are true and the echo runs.
🔧 Debug
expert2:00remaining
Why does this if-then-fi script fail to run?
What error will this script produce and why?
Bash Scripting
x=3 if [ $x -gt 2 ] then echo "Yes" else echo "No"
Attempts:
2 left
💡 Hint
Check if the if block is properly closed.
✗ Incorrect
The script is missing the closing 'fi' keyword which ends the if statement, causing a syntax error.