Bird
Raised Fist0
Bash Scriptingscripting~15 mins

if-then-fi structure in Bash Scripting - Mini Project: Build & Apply

Choose your learning style10 modes available
Basic Decision Making with if-then-fi Structure in Bash
📖 Scenario: You are writing a simple bash script to check if a number is positive. This is like checking if the temperature outside is above zero before deciding to wear a jacket.
🎯 Goal: Build a bash script that uses the if-then-fi structure to check if a variable number is greater than zero and print a message accordingly.
📋 What You'll Learn
Create a variable number with the value 5
Create a variable threshold with the value 0
Use an if statement with then and fi to check if number is greater than threshold
Print "Number is positive" if the condition is true
Print "Number is not positive" if the condition is false
💡 Why This Matters
🌍 Real World
Scripts often need to make decisions, like checking if a file exists or if a user input meets certain criteria. The if-then-fi structure is the basic way to do this in bash.
💼 Career
Understanding conditional statements in bash is essential for system administrators, DevOps engineers, and anyone automating tasks on Linux or Unix systems.
Progress0 / 4 steps
1
Create the number variable
Create a variable called number and set it to 5.
Bash Scripting
Hint

Use number=5 without spaces around the equals sign.

2
Create the threshold variable
Create a variable called threshold and set it to 0.
Bash Scripting
Hint

Use threshold=0 without spaces around the equals sign.

3
Write the if-then-fi condition
Write an if statement to check if number is greater than threshold. Use then and fi to mark the block. Inside the if, print "Number is positive". Use else to print "Number is not positive" if the condition is false.
Bash Scripting
Hint

Use if [ "$number" -gt "$threshold" ] to compare numbers. Remember to close the block with fi.

4
Run the script and display the output
Run the script and print the output to show the message based on the if condition.
Bash Scripting
Hint

Just run the script. The output should be Number is positive.

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

  1. 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.
  2. 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.
  3. Final Answer:

    To run commands only if a certain condition is true -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    if [ $x -gt 5 ]; then echo "Yes"; fi -> Option C
  4. 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

  1. Step 1: Evaluate the condition

    The variable x is 10. The condition checks if 10 is less than 5, which is false.
  2. 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.
  3. Final Answer:

    Done -> Option D
  4. 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

  1. 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.
  2. Step 2: Confirm no syntax errors

    Newline after condition is valid instead of semicolon. All keywords are present and correctly placed.
  3. Final Answer:

    No error, script is correct -> Option B
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    if [ $((n % 2)) -eq 0 ]; then echo "Even"; else echo "Odd"; fi -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard