作者热门文章
- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我有一个 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/
我是一名优秀的程序员,十分优秀!