Bird
0
0

You want to create a script that reads a list of usernames from users.txt and writes a greeting for each user into greetings.txt. Which script snippet correctly does this?

hard🚀 Application Q15 of 15
Bash Scripting - File Operations in Scripts
You want to create a script that reads a list of usernames from users.txt and writes a greeting for each user into greetings.txt. Which script snippet correctly does this?
Awhile read user; do echo "Hello, $user!" >> greetings.txt; done < users.txt
Bfor user in users.txt; do echo "Hello, $user!" > greetings.txt; done
Ccat users.txt | echo "Hello, $user!" > greetings.txt
Decho "Hello, $(cat users.txt)!" > greetings.txt
Step-by-Step Solution
Solution:
  1. Step 1: Understand reading lines from a file

    The 'while read user; do ... done < users.txt' reads each line (username) from users.txt correctly.
  2. Step 2: Check writing greetings

    Inside the loop, echo appends a greeting line to greetings.txt for each user.
  3. Final Answer:

    while read user; do echo "Hello, $user!" >> greetings.txt; done < users.txt -> Option A
  4. Quick Check:

    Loop reads lines and appends greetings [OK]
Quick Trick: Use while read loop with input redirection and append >> [OK]
Common Mistakes:
MISTAKES
  • Using for loop incorrectly on filename instead of lines
  • Overwriting greetings.txt inside loop instead of appending
  • Misusing cat with echo without looping

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes