A. The second echo overwrites the file, losing the first line
B. The variable filename is not defined correctly
C. Missing execute permission on the script
D. The cat command is incorrect
Solution
Step 1: Check file redirection operators
The first echo writes "Start report" to the file. The second echo uses > again, overwriting the file.
Step 2: Understand the effect on output
Because of overwrite, the first line is lost. Only the date line remains in the file.
Final Answer:
The second echo overwrites the file, losing the first line -> Option A
Quick Check:
Using > twice overwrites file [OK]
Hint: Use >> to append, not > to avoid overwriting [OK]
Common Mistakes:
Assuming > appends instead of overwriting
Thinking variable is undefined
Blaming cat command
5. You want to create a report script that lists all files in the current directory and their sizes, saving to files_report.txt. Which script snippet correctly does this?
hard
A. #!/bin/bash
ls -l > files_report.txt
B. #!/bin/bash
ls -l >> files_report.txt
ls -a > files_report.txt
C. #!/bin/bash
ls -lh > files_report.txt
D. #!/bin/bash
ls -lh > files_report.txt
wc -l >> files_report.txt
Solution
Step 1: Understand the requirements
The report must list files with sizes and save to files_report.txt.
Step 2: Analyze options for correct commands and output
#!/bin/bash
ls -lh > files_report.txt
wc -l >> files_report.txt uses ls -lh for human-readable sizes and writes to the file. Then it appends the line count with wc -l, adding useful info.
Step 3: Check other options
#!/bin/bash
ls -l > files_report.txt lacks human-readable sizes. #!/bin/bash
ls -l >> files_report.txt
ls -a > files_report.txt overwrites file after appending, losing data. #!/bin/bash
ls -lh > files_report.txt misses line count info.
Final Answer:
Option D correctly lists files with sizes and appends line count to the report -> Option D
Quick Check:
ls -lh for sizes + wc -l appended = correct report [OK]
Hint: Use ls -lh for sizes and append extra info with >> [OK]
Common Mistakes:
Overwriting file after appending
Not using human-readable size option
Missing extra useful info like line count
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.