How to stop xargs on first error? How to stop xargs on first error? linux linux

How to stop xargs on first error?


General method

xargs -n 1 sh -c '<your_command> $0 || exit 255' < input

Specific case

xargs -n 1 sh -c 'curl --silent --output /dev/null \    --write-out "%{url_effective}: %{http_code}\n" $0 || exit 255' < pages.txt

Explanation

For every URL in pages.txt, executes sh -c 'curl ... $0 || exit 255' one by one (-n 1) forcing to exit with 255 if the command fails.

From man xargs:

If any invocation of the command exits with a status of 255, xargs will stop immediately without reading any further input. An error message is issued on stderr when this happens.


I haven't found a way to do what you ask for with xargs, but a loop with read might be what you are looking for.

while read URL; do  curl --silent \    --output /dev/null --write-out '%{url_effective}: %{http_code}\n' $URL;  RET=$?;  echo $RET;  if [ $RET -ne 0 ]; then break; fidone < pages.txt