input of while loop to come from output of `command`
This is sh
-compatible:
tcpdump -n -r "$pcap" | while read line; do # somethingdone
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.
UPDATE to reflect @Dennis's comment
This works in bash:
while read line; do ARRAY[$c]="$line" c=$((c+1)) done < <(tcpdump -n -r "$pcap")
If you don't care about being bourne, you can switch to 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
:
for line in $(tcpdump -n -r $pcap) do command done