在一天内的特定时段之间调用curl

我必须使用curl来执行一个URL,如果输出包含一个“hello”string,那么我将成功退出shell脚本,否则我会一直重试到凌晨8点,然后退出并显示一条错误消息不包含那个string。

我下面的脚本,但我不能够了解如何我可以运行循环直到上午8点,如果仍然curl输出不包含“你好”string?

#!/bin/bash while true do curl -s -m 2 "some_url" 2>&1 | grep "hello" sleep 15m done 

因此,如果是下午3点之后,则开始进行curl调用,直到上午8点,如果该curl调用给出“hello”string,则成功,否则在8AM退出之后成功退出,并显示错误消息。

如果是在下午3点之前,它会保持睡眠,直到下午3点。

我必须在脚本中添加这个逻辑,我不能在这里使用cron。

您可以使用以下脚本,使用GNU date进行测试

 #/bin/bash retCode=0 # Initializing return code to of the piped commands while [[ "$(date +"%T")" < '08:00:00' ]]; # loop from current time to next occurence of '08:00:00' do curl -s -m 2 "some_url" 2>&1 | grep "hello" retCode=$? # Storing the return code [[ $retCode ]] && break # breaking the loop and exiting on success sleep 15m done [[ $retCode -eq 1 ]] && echo "String not found" >> /dev/stderr # If the search string is not found till the last minute, print the error message 

我认为你可以使用date +%k检索当前小时,并与上午8点和下午13点进行比较。 代码可能会这样

 hour=`date +%k` echo $hour if [[ $hour -gt 15 || $hour -lt 8 ]]; then echo 'in ranage' else echo 'out of range' fi