简体   繁体   中英

Repeat curl command within a bash script for specific output

I am using openphoto to upload photos from within a bash script. Occasionally the upload fails, but all of the commands execute correctly. The json result provides:

{
"message" : "oauth_problem=signature_invalid&debug_sbs=POST&http...,
"code" : 403,
"result" : null
}

The upload fails because the photo that is being uploaded is located on a remote server and occasionally fails to properly cache the file before uploading to openphoto. It is usually only necessary to repeat the command for it to execute correctly.

My bash loop looks like this:

while read i; 
do ./openphoto -p -X POST -h HOSTNAME -e /photo/upload.json \
-F"photo=@$i" \
-F"tags=$(echo "$i"|cut -d'/' -f 7-|sed 's/\/[^/]*[jJ][pP][gG]//;s/\//,/g')"; 
done < files;

How can I get bash to repeat the command if the output contains a "code" of 403?

How can I get bash to repeat the command if the output contains a "code" of 403?

You could add a line before done :

(( $? == 403 )) && command_to_execute

Or if you wanted to try the command until it succeeded, you could say:

until (( $? == 0 )); do command_to_execute; done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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