检查给定 URLs 的响应代码 - 检查所有 URLs 后脚本不会停止

Chcecking the responde code of given URLs - Script don`t stop after checking all URL`s

我在下面做了一个脚本。该脚本正在检查 A 列中例如 .csv 文件中列出的每个 URL 的响应代码。一切都按我的计划进行,但在检查所有 URL 之后,脚本被冻结。我必须使用 ctrl+c 组合来阻止它。在检查完所有 URL 后,如何让脚本自动结束 运行。

#!/bin/bash
for link in `cat ` ;
do
response=`curl --output /dev/null --silent --write-out %{http_code} $link`;
if [ "$response" == "" ]; then
echo "$link";
fi
done

逐字复制您的代码,并用一些由空格分隔的 url 伪造一个测试文件进行测试,它确实挂了。但是,从 for 末尾删除 </code> 允许脚本完成。</p> <pre><code>for link in `cat `;

您的脚本由于 for link 行中的 </code> 而挂起(当它挂起时,检查 <code>ps aux | grep curl 并且您会发现一个 curl 进程,其响应代码作为最后一个参数).此外,for link in `cat ` 不是您读取和处理文件行的方式。

假设您的 example.csv 文件每行仅包含一个 URL 并且没有其他内容(这基本上使它成为一个纯文本文件),此代码应该可以满足您的要求:

#!/usr/bin/env bash
while read -r link; do
    response=$(curl --output /dev/null --silent --write-out %{http_code} "$link")
    if [[ "$response" == "" ]]; then
        echo "$link"
    fi
done < ""