Bird
0
0

Find the error in this script snippet:

medium📝 Debug Q7 of 15
Bash Scripting - File Operations in Scripts
Find the error in this script snippet:
printf "Result: %d\n" 100 > result.txt
printf "Done" >> result.txt
printf "End" > result.txt
Aprintf cannot write to files
BThe last printf overwrites the file, losing previous content
CThe first printf has wrong format specifier
DMissing newline in second printf causes error
Step-by-Step Solution
Solution:
  1. Step 1: Analyze file redirection in each line

    First printf writes formatted line overwriting file. Second appends. Third overwrites again, erasing previous content.
  2. Step 2: Identify unintended overwrite

    The last line uses > which overwrites, losing earlier lines.
  3. Final Answer:

    The last printf overwrites the file, losing previous content -> Option B
  4. Quick Check:

    Using > overwrites file, >> appends [OK]
Quick Trick: Use >> to append, > overwrites file [OK]
Common Mistakes:
MISTAKES
  • Confusing > and >>
  • Assuming printf can't write to files
  • Ignoring file content loss on overwrite

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes