I am running multiple curl commands in a script for different functions. Once the first curl command response is below 400, then only next curl command should execute. Also at the same time the output of each curl should be appended to the output.txt file.
If curl receives a 400 error (or many other error states see man page) it exits with an exit code of 22
curl -i -H "content-type: application/json" -w "{http_code}" -u "$id:$cred" -H "Accept: application/json" -X POST http://sitename -d '{''}' -o out.json
if [ $? -eq 22 ] ; then # see exit codes section of $( man curl )
curl "fallback_page"
fi
Thank you skrynesaver for your response. How can we just take the inputs to a variable and file through the above command. Reason I am saying this is as the for check is done in another part of the code and I don't want to touch it, just have to pass the res ponse code to it and also save the output of curl to a file( and keep on appending others to the same file)
response=$? would allow you to use the returned exit code later in the program.
Had a quick review of the man page there myself, it seems that you need to invoke curl with the -f flag in order for this error state to be propogated,
Sorry, the suggested will give you a command/curl return code - not "http response"
Looking at man curl:
-w, --write-out <format>
Make curl display information on stdout after a completed trans‐
fer. The format is a string that may contain plain text mixed
with any number of variables. The format can be specified as a
literal "string", or you can have curl read the format from a
file with "@filename" and to tell curl to read the format from
stdin you write "@-".
The variables present in the output format will be substituted
by the value or text that curl thinks fit, as described below.
All variables are specified as %{variable_name} and to output a
normal % you just write them as %%. You can output a newline by
using \n, a carriage return with \r and a tab space with \t.
I think you might be missing a %, i.e. -w '%{http_code}'.