Bird
0
0

You want to check if a file exists and is executable, but only if it is not a directory. Which test condition correctly implements this?

hard🚀 Application Q9 of 15
Bash Scripting - Conditionals
You want to check if a file exists and is executable, but only if it is not a directory. Which test condition correctly implements this?
A[ -x "$file" ] || [ -d "$file" ]
B[ -e "$file" ] && [ -x "$file" ] && ! [ -d "$file" ]
C[ -f "$file" ] && [ -r "$file" ]
D[ -d "$file" ] && [ -x "$file" ]
Step-by-Step Solution
Solution:
  1. Step 1: Understand the requirements

    Check file exists -e, is executable -x, and is NOT a directory ! -d.
  2. Step 2: Analyze options

    [ -e "$file" ] && [ -x "$file" ] && ! [ -d "$file" ] correctly combines all conditions with logical AND and negation. Others miss conditions or use OR incorrectly.
  3. Final Answer:

    [ -e "$file" ] && [ -x "$file" ] && ! [ -d "$file" ] -> Option B
  4. Quick Check:

    Use ! to negate directory test [OK]
Quick Trick: Use ! to exclude directories in file tests [OK]
Common Mistakes:
MISTAKES
  • Using || instead of &&
  • Not negating directory test
  • Confusing -f and -e

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes