Bird
0
0

You want to check if a file data.txt exists and is not empty before processing it in a script. Which condition correctly does this?

hard🚀 Application Q15 of 15
Bash Scripting - Conditionals
You want to check if a file data.txt exists and is not empty before processing it in a script. Which condition correctly does this?
A[ -f data.txt ] && [ -s data.txt ]
B[ -f data.txt -o -s data.txt ]
C[ -f data.txt && -s data.txt ]
D[ -f data.txt ] || [ -s data.txt ]
Step-by-Step Solution
Solution:
  1. Step 1: Understand the tests

    -f checks if file exists and is regular file; -s checks if file size is greater than zero (not empty).
  2. Step 2: Choose correct logical operator

    To require both conditions, use logical AND. Inside a single [ ], use -a; or chain separate tests with &&. Using && directly inside [ ] causes syntax error.
  3. Step 3: Analyze options

    The option with && inside single [ ] has syntax error. Chained tests with && are clear and reliable. Options using -o or || apply OR logic, incorrect.
  4. Final Answer:

    [ -f data.txt ] && [ -s data.txt ] -> Option A
  5. Quick Check:

    Use && to combine separate [ ] tests [OK]
Quick Trick: Use separate [ ] with && for AND conditions [OK]
Common Mistakes:
MISTAKES
  • Using -o or || which means OR, not AND
  • Using && inside a single [ ] (syntax error)
  • Not checking file size with -s

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes