input of while loop to come from output of `command`

后端 未结 4 1712
心在旅途
心在旅途 2021-02-04 02:16
#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=$(         


        
4条回答
  •  一整个雨季
    2021-02-04 02:47

    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 () {
        # 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  
    

提交回复
热议问题