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

时间:2020-12-18 23:17:13
#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  

The following fails to work.

以下无法正常工作。

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

Too few results on Google (doesn't understand what I want to find :( ). I'd like to keep it Bourne-compatible (/bin/sh), but it doesn't have to be.

Google上的结果太少了(不明白我想要找到的内容:()。我想保持Bourne兼容(/ bin / sh),但不一定如此。

4 个解决方案

#1


15  

This is sh-compatible:

这是sh兼容的:

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

However, sh does not have arrays, so you can't have your code like it is in sh. Others are correct in saying both bash and perl are nowadays rather widespread, and you can mostly count on their being available on non-ancient systems.

但是,sh没有数组,所以你不能让你的代码像sh一样。其他人都说正确的说bash和perl现在相当普遍,你可以指望他们可以在非古代系统上使用。

UPDATE to reflect @Dennis's comment

更新以反映@Dennis的评论

#2


14  

This works in bash:

这适用于bash:

while read line; do  
  ARRAY[$c]="$line"
  c=$((c+1))  
done < <(tcpdump -n -r "$pcap")

#3


1  

If you don't care about being bourne, you can switch to Perl:

如果您不关心bourne,可以切换到Perl:

my $pcap="somefile.pcap";
my $counter = 0;
open(TCPDUMP,"tcpdump -n -r $pcap|") || die "Can not open pipe: $!\n";
while (<TCPDUMP>) {
    # At this point, $_ points to next line of output
    chomp; # Eat newline at the end
    $array[$counter++] = $_;
}

Or in shell, use for:

或者在shell中,用于:

for line in $(tcpdump -n -r $pcap)  
do  
 command  
done  

#4


1  

for line in $(tcpdump -n -r $pcap)  
do  
 command  
done 

This isn't exactly doing what I need. But it is close. And Shell compatible. I'm creating HTML tables from the tcpdump output. The for loop makes a new <tr> row for each word. It should make a new row for each line (\n ending). Paste bin script01.sh.

这并不完全符合我的需要。但它很接近。和Shell兼容。我正在从tcpdump输出创建HTML表。 for循环为每个单词创建一个新的行。它应该为每一行创建一个新行(\ n结尾)。粘贴bin script01.sh。

#1


15  

This is sh-compatible:

这是sh兼容的:

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

However, sh does not have arrays, so you can't have your code like it is in sh. Others are correct in saying both bash and perl are nowadays rather widespread, and you can mostly count on their being available on non-ancient systems.

但是,sh没有数组,所以你不能让你的代码像sh一样。其他人都说正确的说bash和perl现在相当普遍,你可以指望他们可以在非古代系统上使用。

UPDATE to reflect @Dennis's comment

更新以反映@Dennis的评论

#2


14  

This works in bash:

这适用于bash:

while read line; do  
  ARRAY[$c]="$line"
  c=$((c+1))  
done < <(tcpdump -n -r "$pcap")

#3


1  

If you don't care about being bourne, you can switch to Perl:

如果您不关心bourne,可以切换到Perl:

my $pcap="somefile.pcap";
my $counter = 0;
open(TCPDUMP,"tcpdump -n -r $pcap|") || die "Can not open pipe: $!\n";
while (<TCPDUMP>) {
    # At this point, $_ points to next line of output
    chomp; # Eat newline at the end
    $array[$counter++] = $_;
}

Or in shell, use for:

或者在shell中,用于:

for line in $(tcpdump -n -r $pcap)  
do  
 command  
done  

#4


1  

for line in $(tcpdump -n -r $pcap)  
do  
 command  
done 

This isn't exactly doing what I need. But it is close. And Shell compatible. I'm creating HTML tables from the tcpdump output. The for loop makes a new <tr> row for each word. It should make a new row for each line (\n ending). Paste bin script01.sh.

这并不完全符合我的需要。但它很接近。和Shell兼容。我正在从tcpdump输出创建HTML表。 for循环为每个单词创建一个新的行。它应该为每一行创建一个新行(\ n结尾)。粘贴bin script01.sh。