linux - 在 bash 脚本中重复 curl 命令以获得特定输出

标签 linux bash curl

我正在使用 openphoto 从 bash 脚本上传照片。有时上传会失败,但所有命令都能正确执行。 json 结果提供:

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

上传失败,因为正在上传的照片位于远程服务器上,并且在上传到 openphoto 之前有时无法正确缓存文件。通常只需重复该命令即可正确执行。

我的 bash 循环如下所示:

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;

如果输出包含“代码”403,如何让 bash 重复该命令?

最佳答案

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

您可以在完成之前添加一行:

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

或者,如果您想尝试该命令直到成功,您可以说:

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

关于linux - 在 bash 脚本中重复 curl 命令以获得特定输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19495410/

相关文章:

linux - 如何对目录中的所有文件执行命令并将每个文件的输出移动到新文件夹?

python - 如何在 python 或 linux 中从文本文件中的某个单词后提取信息?

linux - rust mio 甚至在标准输入上也总是报告

linux - 通过 Cygwin 发送电子邮件

php - curl 连接超时不起作用

php - $_FILES 为空,curl 文件上传

json - 使用 JSON 参数进行 Curl GET。服务器回复为空

c - Linux 设备驱动访问控制

linux - greping 服务器日志时忽略对图像等的请求

bash - 如何确定我所在的当前交互式 shell(命令行)