Linux – How to stop xargs on first error


I have an pages.txt file with 100 URLs inside. I want to check them one by one and fail on the first problem. This is what I'm doing:

cat pages.txt | xargs -n 1 curl --silent \
  --output /dev/null --write-out '%{url_effective}: %{http_code}\n'; echo $?

Exit code is 1, but I see it only when the entire file is done. How to stop earlier, on the first problem?

Best Solution

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


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.