Bird
0
0

What will be the output of this script snippet?

medium📝 Command Output Q4 of 15
Bash Scripting - Conditionals
What will be the output of this script snippet?
file="test.txt"
if [ -f "$file" -a ! -r "$file" ]; then
echo "File exists but not readable"
else
echo "Condition not met"
fi
ASyntax error
BFile exists but not readable
CCondition not met
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the condition

    Checks if file exists (-f) AND file is NOT readable (! -r). Both must be true.
  2. Step 2: Consider typical file permissions

    If file exists and is readable, condition fails. If file doesn't exist, condition fails. So usually condition is false.
  3. Final Answer:

    Condition not met -> Option C
  4. Quick Check:

    AND with negation means both must be true [OK]
Quick Trick: Check each condition carefully with AND and NOT [OK]
Common Mistakes:
MISTAKES
  • Assuming file is unreadable
  • Ignoring negation !
  • Confusing -a with -o

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes