【问题标题】:Extracting http status code from "find -exec curl"从“find -exec curl”中提取 http 状态码
【发布时间】:2018-02-01 00:55:09
【问题描述】:

我正在尝试将“find -exec”上下文中的“curl”中的 http 状态代码提取到变量中。我需要这个来测试失败,然后发布报告并防止脚本再次运行。我目前可以使用 -write-out 提取代码并打印到标准输出,但我需要将其存储在脚本中以备后用。

目前有类似的东西:

find . -cmin -140 -type f -iname '*.gz' -exec curl -T {} --write-out "%{http_code}\n" www.example.com/{} \; 

样本输出:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                             Dload  Upload   Total   Spent    Left  Speed
  101  7778    0     0  101  7778      0  17000 --:--:-- --:--:-- --:--:-- 17000
  000
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                             Dload  Upload   Total   Spent    Left  Speed
  101  7795    0     0  101  7795      0  17433 --:--:-- --:--:-- --:--:-- 17433
  000

“000”是打印到控制台的 http 状态代码。我希望在手动测试此脚本时将 curl 输出保留在控制台窗口中,但我确实需要提取状态代码以供以后在脚本中使用。

【问题讨论】:

  • 请添加输出示例。
  • 不确定它是否很有帮助,但我确实添加了它。如果我不必考虑 curl 的输出,我可以简单地从标准输出重定向所有内容,但我只想要那些单独的部分。

标签: bash shell curl find


【解决方案1】:

find-exec 参数在子进程中运行,然后退出。没有简单的方法可以偷偷出状态来控制find。更好的方法是使用循环。

find . -cmin -140 -type f -iname '*.gz' -print0 |
while read -d $'\0' -r file; do
    status=$(curl -T "$file" --write-out "%{http_code}\n" www.example.com/"$file")
    case $status in
     200) ;;
       *) echo "$0: $file fail $status" >&2; break;;
    esac
done

【讨论】:

    猜你喜欢
    • 2016-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-05
    • 2022-01-04
    • 1970-01-01
    • 2011-02-13
    相关资源
    最近更新 更多