1

I'm trying to extract the http status code from "curl" in the context of a "find -exec" into a variable. 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.

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. 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.

2
  • 1
    add a sample of the output please. Commented Jan 31, 2018 at 16:49
  • Not sure if it's very helpful but I did add it. If I didn't have to account for the output of curl I could simply redirect everything from stdout but I only want those individual pieces. Commented Jan 31, 2018 at 16:54

1 Answer 1

2

The -exec argument to find runs in a subprocess which then exits. There is no simple way to smuggle out status to control 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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.