0
0
Bash Scriptingscripting~20 mins

if-then-fi structure in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
If-Then-Fi Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2: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
ANo output
BLess
CSyntax error
DGreater
Attempts:
2 left
💡 Hint
Remember the condition checks if x is greater than 3.
💻 Command Output
intermediate
2: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
AGreater
BNo output
CSyntax error
D2
Attempts:
2 left
💡 Hint
The condition checks if 2 is greater than 3.
📝 Syntax
advanced
2: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
AReplace [ ] with (( ))
BRemove the fi keyword
CAdd 'then' after the if condition
DAdd a semicolon after echo
Attempts:
2 left
💡 Hint
The if statement needs a 'then' keyword before commands.
🚀 Application
advanced
2: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
ABoth conditions met
BOnly first condition met
COnly second condition met
DNo output
Attempts:
2 left
💡 Hint
Both x and y satisfy their conditions.
🔧 Debug
expert
2: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"
ASyntax error: missing fi keyword
BNo output
CRuntime error: invalid condition
DOutputs 'Yes' correctly
Attempts:
2 left
💡 Hint
Check if the if block is properly closed.