gpt4 book ai didi

linux - 如何在第一个错误时停止 xargs?

转载 作者:IT老高 更新时间:2023-10-28 12:41:16 32 4
gpt4 key购买 nike

我有一个 pages.txt 文件,里面有 100 个 URL。我想一个一个地检查它们并在第一个问题上失败。这就是我正在做的:

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

退出代码是1,但我只有在整个文件完成后才能看到它。第一个问题,如何提前停止?

最佳答案

一般方法

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

具体情况

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

说明

对于 pages.txt 中的每个 URL,执行 sh -c 'curl ... $0 || exit 255' 如果命令失败,则一一(-n 1)强制以255 退出。

来自 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.

关于linux - 如何在第一个错误时停止 xargs?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26484443/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com