Q
在while循环
0
A
回答
4
貌似你试图使用process substitution:
lines=5
while read line ; do
let ++lines
echo "$lines $line" # Number each line
# Other operations on $line and $lines
done < <(grep ...)
echo "Total: $lines lines"
提供grep
实际上返回一些输出线,其结果应该是这样的:
6: foo
7: bar
Total: 7 lines
这与grep ... | while ...
略有不同:在前者中,grep
在subshell中运行,而在拿铁r while
循环处于子外壳中。如果你想在循环中保持一些状态,这通常只是相关的 - 在这种情况下,你应该使用第一种形式。
在另一方面,如果你写
lines=5
grep ... | while read line ; do
let ++lines
echo "$lines $line" # Number each line
# Other operations on $line and $lines
done
echo "Total: $lines lines"
的结果将是:
6: foo
7: bar
Total: 5 lines
哎哟!计数器被传递给子shell(管道的第二部分),但它不会返回到父shell。
+0
所以这可以用来单独处理每一行,但我不能在循环中使用计数器?你能解释一下这个语法吗? – Xaero182 2012-04-16 15:03:25
3
grep
是一个命令,但done < grep
告诉shell使用名为grep的文件作为输入。你需要的东西,如:
grep ... | while read line ; do
...
done
+0
+1:D以秒为单位打败我 – 2012-04-16 10:10:06
相关问题
- 1. while while循环
- 2. while while循环
- 3. 在while循环
- 4. 在while循环
- 5. 在while循环
- 6. 在while循环
- 7. 在while循环
- 8. Perl while while循环只在for循环中循环一次
- 9. Break while while循环
- 10. while while循环Java
- 11. while while循环? (JAVA)
- 12. fflush while while循环
- 13. JAVA while while循环
- 14. Scheme while while循环
- 15. while while循环+在while循环中创建aproccess
- 16. Java while while循环不循环
- 17. while循环和While循环的退出
- 18. Python的while循环while循环
- 19. PHP for循环while while循环
- 20. while循环在php
- 21. CIN在while循环
- 22. 在C++ while循环
- 23. while循环在MIPS
- 24. while循环在MIPS
- 25. 循环while循环mysqli_fetch_array()
- 26. For循环到While循环
- 27. For循环vs While循环
- 28. 循环和while循环
- 29. for循环while循环
- 30. 内循环while循环内循环
你想做什么? grep从每行读入的东西? – Brady 2012-04-16 10:10:33