AbtPst AbtPst - 7 months ago 110
Bash Question

Alternative to xargs for processing pipeline

I am trying to run a curl command which will need arguments coming in from a pipe

some operation | sort | head -n -2 | curl -s "blah/blah/<argument goes here>/blah"


i cant figure out how to pass each argument one at a time. One way that i found was

some operation | sort | head -n -2 | xargs -l "$" curl -s "blah/blah/$/blah"


but i get

xargs: $: No such file or directory


what does it mean? why is it trying to read $ as a file?

Answer

See BashFAQ #1 for the best-practices way to read a string line-by-line:

#!/bin/bash
#      ^^^^ - not /bin/sh, or <() will be a syntax error

while IFS= read -r line; do
  curl -s "blah/blah/$line/blah"
done < <(some operation | sort | head -n -2)