What is the purpose of the if-then-fi structure in bash scripting?
It allows the script to make decisions by running commands only if a certain condition is true.
Click to reveal answer
beginner
Write the basic syntax of an if-then-fi statement in bash.
if [ condition ]; then commands fi
Click to reveal answer
beginner
In bash scripting, what does the fi keyword do?
It marks the end of the if block, telling the shell that the conditional commands are finished.
Click to reveal answer
intermediate
How do you check if a file exists using if-then-fi in bash?
if [ -e filename ]; then echo "File exists" fi
Click to reveal answer
beginner
Why is it important to have spaces around the square brackets in if [ condition ]?
Because [ is a command and needs spaces to separate it from the condition and brackets, otherwise the shell will give an error.
Click to reveal answer
What keyword ends an if block in bash scripting?
Afi
Bend
Cdone
Dstop
✗ Incorrect
The fi keyword is used to close an if block in bash.
Which of these is the correct way to start an if statement in bash?
Aif ( condition ) then
Bif [ condition ]; then
Cif { condition } then
Dif condition then
✗ Incorrect
The correct syntax uses square brackets with spaces and a semicolon before then: if [ condition ]; then.
What happens if the condition in an if statement is false?
AThe commands inside <code>then</code> run
BThe script restarts
CThe script stops immediately
DThe commands inside <code>then</code> do not run
✗ Incorrect
If the condition is false, the commands inside the then block are skipped.
Which symbol is used to test conditions inside the if statement?
A[]
B()
C{}
D<>
✗ Incorrect
Square brackets [] are used to test conditions in bash if statements.
How do you write an if statement that checks if a variable x equals 5?
Aif [ $x == 5 ]; then
Bif ( $x = 5 ) then
Cif [ $x = 5 ]; then
Dif $x == 5 then
✗ Incorrect
In bash, use single equals = inside square brackets with spaces: if [ $x = 5 ]; then.
Explain how the if-then-fi structure works in bash scripting.
Think about how you decide to do something only if a condition is met.
You got /3 concepts.
Write a simple bash script using if-then-fi to check if a file named 'data.txt' exists and print a message.
Use the -e option inside the condition to check for file existence.
You got /4 concepts.
Practice
(1/5)
1. What is the purpose of the if-then-fi structure in a bash script?
easy
A. To run commands only if a certain condition is true
B. To repeat commands multiple times
C. To define a function
D. To declare a variable
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 A
Quick Check:
if-then-fi controls conditional execution = D [OK]
Hint: If you want to do something only when true, use if-then-fi [OK]
Common Mistakes:
Confusing if-then-fi with loops
Thinking if-then-fi defines functions
Using if-then-fi to declare variables
2. Which of the following is the correct syntax for a simple if statement in bash?
easy
A. if $x > 5 then echo "Yes" fi
B. if ($x > 5) { echo "Yes" }
C. if [ $x -gt 5 ]; then echo "Yes"; fi
D. if $x -gt 5 echo "Yes" fi
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 C
Quick Check:
Correct bash if syntax = C [OK]
Hint: Remember: if [ condition ]; then ... fi with spaces [OK]
Common Mistakes:
Missing spaces inside [ ]
Omitting 'then' keyword
Using wrong brackets or symbols
3. What will be the output of this script?
#!/bin/bash
x=10
if [ $x -lt 5 ]; then
echo "Less"
fi
echo "Done"
medium
A. No output
B. Less
Done
C. Less
D. 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 D
Quick Check:
Condition false, only "Done" prints = A [OK]
Hint: If condition false, skip if block, run commands after fi [OK]
Common Mistakes:
Assuming if block runs even if condition false
Confusing -lt with -gt
Ignoring commands after fi
4. Identify the error in this bash script snippet:
if [ $num -eq 5 ]
then
echo "Number is five"
fi
medium
A. Missing semicolon or newline after condition
B. No error, script is correct
C. Missing fi keyword
D. Wrong comparison operator
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 B
Quick Check:
Proper if-then-fi syntax = B [OK]
Hint: Newline after condition is allowed instead of semicolon [OK]
Common Mistakes:
Thinking newline after condition is error
Confusing -eq with other operators
Forgetting fi keyword
5. You want to write a script that prints "Even" if a number stored in n is even, and "Odd" otherwise. Which of these is the correct if-then-fi structure to do this?
hard
A. if [ $((n % 2)) -eq 0 ]; then echo "Even"; else echo "Odd"; fi
B. if [ $n % 2 == 0 ]; then echo "Even"; else echo "Odd"; fi
C. if (( n % 2 == 0 )) then echo "Even"; else echo "Odd"; fi
D. if [ $n / 2 -eq 0 ]; then echo "Even"; else echo "Odd"; fi
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 A
Quick Check:
Modulo with $(( )) inside [ ] and proper if-then-fi = A [OK]
Hint: Use $(( )) for math inside [ ] in if-then-fi [OK]
Common Mistakes:
Using % inside [ ] without $(( ))
Using == instead of -eq for numbers
Confusing division with modulo
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.