Bird
Raised Fist0
Bash Scriptingscripting~10 mins

if-then-fi structure in Bash Scripting - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the variable num is equal to 5.

Bash Scripting
if [ "$num" [1] 5 ]; then
  echo "Number is five"
fi
Drag options to blanks, or click blank then click option'
A==
B=
C-eq
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using string comparison operators like '=' or '==' for numbers.
Forgetting spaces around brackets and operators.
2fill in blank
medium

Complete the code to check if the file myfile.txt exists.

Bash Scripting
if [1] myfile.txt; then
  echo "File exists"
fi
Drag options to blanks, or click blank then click option'
Atest -s
Btest -d
Ctest -f
Dtest -e
Attempts:
3 left
💡 Hint
Common Mistakes
Using -d which checks only directories.
Using -s which checks if file is not empty.
3fill in blank
hard

Fix the error in the if statement to correctly check if count is greater than 10.

Bash Scripting
if [ $count [1] 10 ]; then
  echo "Count is large"
fi
Drag options to blanks, or click blank then click option'
A<
B-gt
C==
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' inside single brackets which is a string comparison and needs escaping.
Using '==' which is for string equality.
4fill in blank
hard

Fill both blanks to check if the variable word is not empty and equals "hello".

Bash Scripting
if [ -n [1] ] && [ [2] = "hello" ]; then
  echo "Greeting detected"
fi
Drag options to blanks, or click blank then click option'
A$word
B-z
D"hello"
Attempts:
3 left
💡 Hint
Common Mistakes
Using -z which checks for empty string.
Not using the variable with a $ sign.
5fill in blank
hard

Fill all three blanks to check if num is between 1 and 10 inclusive.

Bash Scripting
if [ [1] -ge 1 ] && [ [2] [3] 10 ]; then
  echo "Number is between 1 and 10"
fi
Drag options to blanks, or click blank then click option'
A$num
C-le
D-lt
Attempts:
3 left
💡 Hint
Common Mistakes
Using string comparison operators instead of numeric ones.
Using -lt instead of -le for the upper bound.

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