Bird
0
0

You want to write a script that reads lines from a file until it finds the word 'STOP'. Which while loop structure is best?

hard🚀 Application Q8 of 15
Bash Scripting - Loops
You want to write a script that reads lines from a file until it finds the word 'STOP'. Which while loop structure is best?
Awhile [[ $line == STOP ]]; do read line; echo $line; done < file.txt
Bwhile [ $line != STOP ]; do read line; echo $line; done < file.txt
Cwhile true; do read line; echo $line; done < file.txt
Dwhile read line; do if [[ $line == STOP ]]; then break; fi; echo $line; done < file.txt
Step-by-Step Solution
Solution:
  1. Step 1: Understand reading lines with while

    Use 'while read line' to read each line from file.
  2. Step 2: Stop loop on 'STOP'

    Check if line equals 'STOP' and break loop if true.
  3. Final Answer:

    while read line; do if [[ $line == STOP ]]; then break; fi; echo $line; done < file.txt -> Option D
  4. Quick Check:

    Read lines and break on STOP = while read line; do if [[ $line == STOP ]]; then break; fi; echo $line; done < file.txt [OK]
Quick Trick: Use 'break' inside while to stop on condition [OK]
Common Mistakes:
MISTAKES
  • Checking condition before reading line
  • Infinite loop without break
  • Wrong condition syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes