@Socowi引导我找到了完美的解决方案,您可以在问题的底部看到它:
(1)这里是一个脚本的实际例子,它的内容包括10个滚动的帖子请求,每个请求都会在我的网站上发布一个不同的评论。
#!/bin/bash
curl "https://mywebsite.com/comment... ...&text=Test-1"; sleep 60;
curl "https://mywebsite.com/comment... ...&text=Test-2"; sleep 60;
curl "https://mywebsite.com/comment... ...&text=Test-3"; sleep 60;
curl "https://mywebsite.com/comment... ...&text=Test-4"; sleep 60;
curl "https://mywebsite.com/comment... ...&text=Test-5"; sleep 60;
curl "https://mywebsite.com/comment... ...&text=Test-6"; sleep 60;
curl "https://mywebsite.com/comment... ...&text=Test-7"; sleep 60;
curl "https://mywebsite.com/comment... ...&text=Test-8"; sleep 60;
curl "https://mywebsite.com/comment... ...&text=Test-9"; sleep 60;
curl "https://mywebsite.com/comment... ...&text=Test-10"; sleep 60;
(2)当它顺利运行时,下面是终端的样子:
(3) 问题:随机间隔的会出错,而不是上面屏幕截图上显示的内容,我将开始获得大量的文本,其中包含诸如“出了问题”之类的单词。例如,它可以很好地执行前6个curl命令,并且在7号会有一个不好的响应.该脚本将继续运行,运行第8 curl命令,并获得终端中显示的相同错误,脚本将一直持续到结束时,让我完成部分完成的工作。
(4) 所需的解决方案:--我只希望脚本在终端中抛出错误时暂停/等待300秒钟,然后继续运行脚本中的下一个curl命令。等待确实有帮助,但我现在必须手动完成。请帮助我解决方案,如何正确修改我的脚本,以实现同样的。
谢谢!
编辑:由于@Socowi:解决了我的问题
#!/bin/bash
for i in {1..10}; do
if curl "https://mywebsite.com/comment... ...&text=Test-$i" | grep -qF '"status":"ERROR"'; then
sleep 300 # there was an error
else
sleep 60 # no error given
fi
done
exec $SHELL
发布于 2021-04-24 20:19:16
通常,您可以使用if curl ...
检查退出状态,并相应地调整睡眠时间。但是,在您的示例中,curl
成功地获得了一个响应。curl
并不关心响应的内容,但是您可以自己检查内容。在您的例子中,json的工具将是解析响应的正确方法,但是一个讨厌的grep
也会完成这项工作。
因为您想要将响应打印到终端,所以我们使用一个变量,以便可以打印响应并在其上使用grep
。
#!/bin/bash
for i in {1..10}; do
response=$(curl "https://...&text=Test-$i")
echo "$response"
if grep -qF '"status":"ERROR"' <<< "$response"; then
sleep 300 # there was an error
else
sleep 60 # everything ok
fi
done
https://stackoverflow.com/questions/67247009
复制相似问题