#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
那样拥有你的代码.其他人说的都是正确的 bash
和 perl
现在相当普遍,并且您大多可以指望它们在非古代系统上可用。更新 反射(reflect)@Dennis 的评论
关于shell - while 循环的输入来自 `command` 的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2983213/