Ever wanted to run some command on a list of inputs? That is to say, execute said command on each item in a list. That could be running a command on a list of file names, running a command multiple times each with at different input value etc.

This can be done quite easily by shoe-horning a while loop fed by pipe onto a single line in the format:

1
<in_command> | while read in; do <proc_command> $in; done

Here in_command is what feeds the list into the while loop, in is the current item in the list being operated on and proc_command is the command that does the processing on said item.

Examples

Deleting a list of files

Here we pipe a list of file names into the while loop where rm is run on each iteration thereby deleting each file.

1
cat files.txt | while read file; do rm $file; done

Ping a bunch of IPs

Here we pipe a list of IP addresses into the while loop where ping is run on each iteration for 5 pings.

1
cat ips.txt | while read ip; do ping -c 5 $ip; done

:)