Bird
0
0

What will be the output of this script if myfile.txt exists and is readable but not writable?

medium📝 Command Output Q13 of 15
Bash Scripting - Conditionals
What will be the output of this script if myfile.txt exists and is readable but not writable?
if [ -r myfile.txt ]; then echo "Readable"; else echo "Not readable"; fi
if [ -w myfile.txt ]; then echo "Writable"; else echo "Not writable"; fi
AReadable\nWritable
BNot readable\nWritable
CReadable\nNot writable
DNot readable\nNot writable
Step-by-Step Solution
Solution:
  1. Step 1: Check readability condition

    The file exists and is readable, so [ -r myfile.txt ] is true, printing "Readable".
  2. Step 2: Check writability condition

    The file is not writable, so [ -w myfile.txt ] is false, printing "Not writable".
  3. Final Answer:

    Readable\nNot writable -> Option C
  4. Quick Check:

    Readable true, writable false = A [OK]
Quick Trick: Check each permission separately with -r and -w [OK]
Common Mistakes:
MISTAKES
  • Assuming writable if readable
  • Mixing up -r and -w operators
  • Ignoring else branch output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes