Bird
0
0

Which condition correctly checks if the file report.log exists and is not empty in bash?

hard🚀 Application Q9 of 15
Bash Scripting - Conditionals
Which condition correctly checks if the file report.log exists and is not empty in bash?
A[ -f report.log ]
B[ -e report.log -a -s report.log ]
C[ -s report.log ]
D[ -r report.log ]
Step-by-Step Solution
Solution:
  1. Step 1: Understand file test operators

    - -s returns true if file exists and size is greater than zero.
    - -e checks existence only.
    - -f checks if it is a regular file.
    - -r checks readability.
  2. Step 2: Choose the correct test

    Since we want to check existence and non-empty, -s alone suffices.
  3. Final Answer:

    [ -s report.log ] -> Option C
  4. Quick Check:

    Test with an empty and non-empty file [OK]
Quick Trick: Use -s to check file exists and not empty [OK]
Common Mistakes:
MISTAKES
  • Using -e alone which doesn't check file size
  • Combining -e and -s unnecessarily
  • Using -f which ignores file size

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes