117-102 · Question #756
What keyword is missing from this code sample of a shell script? ____ i in *.txt; do echo $i done
The correct answer is A. for. Exam Questions, Study Guides, Practice Tests. Lead the way to help you pass any IT Certification exams, 100% Pass Guaranteed or Full Refund. Especially Cisco, CompTIA, Citrix, EMC, HP, Oracle, VMware, Juniper, Check Point, LPI, Nortel, EXIN and so on. Our Slogan: First Test…
Question
Options
- Afor
- Bloop
- Cuntil
- Dwhile
How the community answered
(60 responses)- A78% (47)
- B12% (7)
- C7% (4)
- D3% (2)
Explanation
Exam Questions, Study Guides, Practice Tests. Lead the way to help you pass any IT Certification exams, 100% Pass Guaranteed or Full Refund. Especially Cisco, CompTIA, Citrix, EMC, HP, Oracle, VMware, Juniper, Check Point, LPI, Nortel, EXIN and so on. Our Slogan: First Test, First Pass. Help you to pass any IT Certification exams at the first try. You can reach us at any of the email addresses listed below. Any problems about IT certification or our products, you could rely upon us, we will give you satisfactory answers in 24 hours.
Topics
Community Discussion
6The answer is A, for. That construct, "for i in *.txt", is the standard Bash way to loop over a list of items, in this case every .txt file in the current directory, and run the echo command on each one.
Yusuf is right that A is the answer, but worth adding that the "for...in" loop here works exactly like sorting mail into slots, where the shell first expands the wildcard into the full pile of envelopes (filenames), then hands each one to echo one at a time, so if there are no .txt files the loop just gets a literal "*.txt" string unless nullglob is set.
The "in" after the variable locks this to for, answer is A.
The "in" keyword is definitely the tell, though it helps to also remember that "of" goes with for...of so you have a quick mental pair to anchor both loop types at once.
Think of it like a vending machine that keeps cycling until it runs out of snacks, and that is exactly what "until" does here, it keeps looping through each .txt file until the list is exhausted, which fits the pattern of iterating over a group of items perfectly. I am going with C on this one, the syntax lines up and the logic makes sense when you picture that vending machine working through its inventory one item at a time.
Hey Mateus, I get the vending machine picture but "until" actually checks a condition and keeps looping while that condition is false, so it is not really built for walking through a list of items one by one the way "for" is, and a senior on my team drilled into me that "for file in *.txt" is the classic pattern when you just need to touch each file in a set.