Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

#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.

like image 886
Felipe Alvarez Avatar asked Jun 06 '10 05:06

Felipe Alvarez


1 Answers

This is sh-compatible:

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.

UPDATE to reflect @Dennis's comment

like image 150
Amadan Avatar answered Sep 22 '22 05:09

Amadan