简体   繁体   English

从“ find -exec curl”中提取http状态代码

[英]Extracting http status code from “find -exec curl”

I'm trying to extract the http status code from "curl" in the context of a "find -exec" into a variable. 我正在尝试在“ find -exec”的上下文中从“ curl”中提取http状态代码。 I need this to test for failure in order to then issue reports and prevent the script from running again. 我需要此工具来测试是否失败,以便随后发布报告并阻止脚本再次运行。 I can currently extract the code and print to stdout using -write-out but I need it stored within the script for later use. 我目前可以提取代码并使用-write-out打印到stdout,但我需要将其存储在脚本中以备后用。

Currently have something similar to: 目前有类似的东西:

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

Sample output: 样本输出:

  % 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

The '000' is the http status code printing to the console. '000'是打印到控制台的http状态代码。 I would like the keep the curl output in the console window for when this script is manually tested, but I do need to extract the status code for later use in the script. 我希望在手动测试此脚本时将curl输出保留在控制台窗口中,但是我确实需要提取状态代码以供以后在脚本中使用。

The -exec argument to find runs in a subprocess which then exits. find-exec参数在子进程中运行,然后退出。 There is no simple way to smuggle out status to control find . 没有简单的方法走私状态来控制find A better approach is to use a loop. 更好的方法是使用循环。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM