Command Output पर Looping
In this page:
while IFS= read -r line; do
commands using "$line"
done <<< "$data"
command | while IFS= read -r line; do
commands
done
The while read Idiom
while IFS= read -r line; do ... done <<< "$data" text को line by line process करने का standard safe तरीका है: -r backslash interpretation disable करता है, और IFS= हर line से leading/trailing whitespace strip होने से रोकता है।
इसे <<< (एक here-string) के through feed करना loop को current shell में चलाता है, एक subshell में नहीं।
उदाहरण: The while read Idiom
#!/bin/bash
data=$'line one\nline two\nline three'
while IFS= read -r line; do
echo "Processing: $line"
done <<< "$data"
Login to try C/C++/Java/PHP code in the editor
Reading a File Line by Line
वही while read idiom input redirection (< file) उपयोग करके एक file के against सीधे काम करता है, here-string के बजाय, बिना कभी पूरी file को पहले एक variable में load किए।
यह बड़ी files के लिए भी अच्छी तरह scale करता है क्योंकि हर line एक समय में एक पढ़ी जाती है।
उदाहरण: Reading a File Line by Line
#!/bin/bash
printf "apple\nbanana\ncherry\n" > fruits.txt
while IFS= read -r line; do
echo "Fruit found: $line"
done < fruits.txt
Login to try C/C++/Java/PHP code in the editor
Looping Over Command Output with Process Substitution
while read को एक command के output से process substitution, < <(command), के through feed करना उस subshell problem से बचता है जो piping (command | while read) करती है, क्योंकि loop अब भी current shell में चलता है।
इसका मतलब है loop के अंदर set किए variables इसके खत्म होने के बाद भी visible रहते हैं।
उदाहरण: Looping Over Command Output with Process Substitution
#!/bin/bash
total=0
while IFS= read -r num; do
total=$((total + num))
done < <(printf "1\n2\n3\n")
echo "Total after loop: $total"
Login to try C/C++/Java/PHP code in the editor
for word in $(command) for Simple Word Lists
जब एक command का output simple, whitespace-separated single-word tokens होना जाना जाता हो बिना embedded spaces के, for word in $(command); do ... done उनके ऊपर iterate करने का एक quick तरीका है।
किसी भी line-structured या spaces वाली चीज़ के लिए, while read safer और ज़्यादा correct choice है।
while read के लिए reach करें।उदाहरण: for word in $(command) for Simple Word Lists
#!/bin/bash
for status_code in $(printf "200 404 500"); do
echo "HTTP status: $status_code"
done
Login to try C/C++/Java/PHP code in the editor
- Multi-word lines के ऊपर loop करने के लिए
for line in $(command)उपयोग करना; यह हर space और newline पर word-split करता है, एक से ज़्यादा word वाली किसी भी line को mangle करते हुए -- line-based processing के लिएwhile readcorrect tool है। - एक
while readloop में pipe करना और फिर confused होना कि loop के अंदर set किए variables बाद में disappear हो जाते हैं; एक pipe के receiving end वाला loop एक subshell में चलता है, तो इसके variable changes parent shell में persist नहीं करते (process substitution इससे बचता है, लेकिन यह एक ज़्यादा advanced fix है)। - यह भूल जाना कि
readby default leading/trailing whitespace strip करता है और backslashes interpret करता है जब तक-r(raw mode) उपयोग न हो औरIFSadjust न हो ---rछोड़ देना silently backslashes वाली lines corrupt कर सकता है।
while IFS= read -r line; do ... done <<< "$data"(या< file) text को line by line process करने का standard, safe idiom है।-rreadको backslash escapes interpret करने से रोकता है;IFS=इसे हर line से leading/trailing whitespace trim करने से रोकता है।- एक
while readloop को एक pipe के through feed करना इसे एक subshell में डालता है, तो यह set किए variables loop के बाद खो जाते हैं; input के लिए इसके बजाय<<<या< fileउपयोग करना इसे current shell में रखता है। for word in $(command)को सिर्फ simple, known-safe, single-word-per-item output के लिए reserved रखना चाहिए -- किसी भी line-oriented चीज़ के लिए,while readprefer करें।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: