Bird
Raised Fist0
Bash Scriptingscripting~5 mins

if-then-fi structure in Bash Scripting

Choose your learning style10 modes available
Introduction
The if-then-fi structure helps your script make decisions by running commands only when certain conditions are true.
Check if a file exists before trying to read it.
Run a command only if a user is logged in.
Decide what message to show based on a number's value.
Verify if a program is installed before using it.
Syntax
Bash Scripting
if [ condition ]
then
  commands
fi
The 'fi' word ends the if block; it's 'if' spelled backward.
Conditions are placed inside square brackets with spaces around them.
Examples
Checks if 'myfile.txt' exists as a file, then prints a message.
Bash Scripting
if [ -f myfile.txt ]
then
  echo "File exists"
fi
Greets the user only if their username is 'admin'.
Bash Scripting
if [ "$USER" = "admin" ]
then
  echo "Welcome, admin!"
fi
Prints a message if the variable 'number' is more than 10.
Bash Scripting
if [ "$number" -gt 10 ]
then
  echo "Number is greater than 10"
fi
Sample Program
This script asks the user to enter a number. If the number is greater than zero, it prints a message saying it's positive.
Bash Scripting
#!/bin/bash

read -p "Enter a number: " num

if [ "$num" -gt 0 ]
then
  echo "You entered a positive number."
fi
OutputSuccess
Important Notes
Always put spaces after '[' and before ']' in conditions.
Use '-f' to check if a file exists, '-d' for directories, and '-gt' for numeric comparisons.
Remember to make your script executable with 'chmod +x script.sh' before running.
Summary
The if-then-fi structure lets your script choose actions based on conditions.
Conditions go inside square brackets with spaces around them.
End the if block with 'fi' to close it properly.

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