Bird
0
0

You want to write a script that checks if a path is a regular file and is readable and writable. Which combined test condition is correct?

hard🚀 Application Q8 of 15
Bash Scripting - Conditionals
You want to write a script that checks if a path is a regular file and is readable and writable. Which combined test condition is correct?
A[ -f "$path" ] || [ -r "$path" ] || [ -w "$path" ]
B[ -d "$path" ] || [ -r "$path" ] && [ -w "$path" ]
C[ -e "$path" ] && [ -x "$path" ]
D[ -f "$path" ] && [ -r "$path" ] && [ -w "$path" ]
Step-by-Step Solution
Solution:
  1. Step 1: Understand the requirements

    The script must check if the path is a regular file -f and has read -r and write -w permissions.
  2. Step 2: Analyze each option

    [ -f "$path" ] && [ -r "$path" ] && [ -w "$path" ] uses logical AND to combine all required tests correctly. Others use wrong operators or test directories or executability.
  3. Final Answer:

    [ -f "$path" ] && [ -r "$path" ] && [ -w "$path" ] -> Option D
  4. Quick Check:

    Combine tests with && for all conditions [OK]
Quick Trick: Use && to combine multiple file tests in bash [OK]
Common Mistakes:
MISTAKES
  • Using || instead of &&
  • Testing directory instead of file
  • Missing quotes around variables

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes