How to build and run a shell command based on the contents of several lines in Vim?

I suggest you use xargs -L1

Example:

:%!xargs -L1 wc -l

Basically xargs [cmd] will run the following [cmd] with multiple parameters from multiple lines. But with the -L1 argument the [cmd] will be executed command for each line.

If you need to supply a certain order to the arguments for your command you can use xargs's -I option to supply a pattern to be replaced by the argument list.

Example:

:%!xargs -L1 -I {} rake {} --trace

If you feel very adventurous you can just execute the code directly like so:

:%!bash

echo map(getline("'<", "'>"), 'system(v:val)[:-2]')

:h map()
:h system()
" [:-2] ~> chomp

You can use line insertion functions instead of :echo if you prefer (:put, setline(), etc).