在一天内的特定时间段内呼叫卷曲

时间:2016-09-28 05:08:04

标签: linux bash shell curl

我必须使用curl执行一个URL,如果输出包含一个“hello”字符串,那么我将成功退出shell脚本,否则我将继续重试直到早上8点,然后退出并显示错误消息它仍然不包含该字符串。

我得到了以下脚本,但是我无法理解如何在循环中运行到上午8点,如果仍然卷曲输出不包含“hello”字符串?

#!/bin/bash

while true
do
    curl -s -m 2  "some_url" 2>&1 | grep "hello"
    sleep 15m
done

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

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

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

2 个答案:

答案 0 :(得分:1)

我认为您可以使用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

答案 1 :(得分:1)

您可以使用以下脚本,使用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
相关问题