簡體   English   中英

從“ find -exec curl”中提取http狀態代碼

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

我正在嘗試在“ find -exec”的上下文中從“ curl”中提取http狀態代碼。 我需要此工具來測試是否失敗,以便隨后發布報告並阻止腳本再次運行。 我目前可以提取代碼並使用-write-out打印到stdout,但我需要將其存儲在腳本中以備后用。

目前有類似的東西:

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輸出保留在控制台窗口中,但是我確實需要提取狀態代碼以供以后在腳本中使用。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM