Bird
0
0

You want to write a bash condition that checks if a file log.txt exists AND is either writable OR executable. Which is the correct syntax inside [ ]?

hard🚀 Application Q8 of 15
Bash Scripting - Conditionals
You want to write a bash condition that checks if a file log.txt exists AND is either writable OR executable. Which is the correct syntax inside [ ]?
A[ -e log.txt && ( -w log.txt || -x log.txt ) ]
B[ -e log.txt -a \( -w log.txt -o -x log.txt \) ]
C[ -e log.txt -o -w log.txt -a -x log.txt ]
D[ -e log.txt && -w log.txt -o -x log.txt ]
Step-by-Step Solution
Solution:
  1. Step 1: Understand operator precedence and grouping

    Inside [ ], use \( and \) to group conditions. AND is -a, OR is -o.
  2. Step 2: Write condition for file exists AND (writable OR executable)

    Correct syntax is [ -e log.txt -a \( -w log.txt -o -x log.txt \) ] to group OR inside AND.
  3. Final Answer:

    [ -e log.txt -a \( -w log.txt -o -x log.txt \) ] -> Option B
  4. Quick Check:

    Group OR with \( \) inside [ ] [OK]
Quick Trick: Use \( \) to group OR inside AND in [ ] [OK]
Common Mistakes:
MISTAKES
  • Missing grouping parentheses
  • Using && or || inside [ ]
  • Wrong operator order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes