shell - while 循环的输入来自 `command` 的输出

标签 shell loops while-loop

#I used to have this, but I don't want to write to the disk
#
pcap="somefile.pcap"
tcpdump -n -r $pcap > all.txt
while read line; do  
  ARRAY[$c]="$line"
  c=$((c+1))  
done < all.txt  

以下无法工作。
# I would prefer something like...
#
pcap="somefile.pcap"
while read line; do  
  ARRAY[$c]="$line"
  c=$((c+1))  
done < $( tcpdump -n -r "$pcap" )

Google 上的结果太少(不明白我想找到什么 :( )。我想保持它与 Bourne 兼容 (/bin/sh),但不是必须的。

最佳答案

这是sh -兼容的:

tcpdump -n -r "$pcap" | while read line; do  
  # something
done

然而,sh没有数组,所以你不能像 sh 那样拥有你的代码.其他人说的都是正确的 bashperl现在相当普遍,并且您大多可以指望它们在非古代系统上可用。

更新 反射(reflect)@Dennis 的评论

关于shell - while 循环的输入来自 `command` 的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2983213/

相关文章:

linux - bash 中的静默 while 循环

winforms - 具有系统窗口表单对象的PowerShell如何显示命令 Pane 或确认框

c - c 中的 exit(0) 仅偶尔有效

C:如何在我的代码中打破这个循环?

R:将元素添加到 tryCatch 循环中的列表

php - PHP while 循环中没有条件?

shell - Applescript Shell 脚本进度

shell - 仅当存在第 3 个模式时 SED 删除(包括)2 个模式之间

python - 如何使用python中的sh库删除目录中所有带*的文件?

c - 在c中使用指针的数组总和