我必须使用curl执行URL,如果输出包含"hello"字符串,那么我将成功退出shell脚本,否则我一直重试到早上8点,然后退出错误消息,如果它仍然不包含该字符串。
我得到下面的脚本,但我无法理解我如何能运行while循环,直到8 am,如果仍然卷曲输出不包含"hello"字符串?
#!/bin/bash
while true
do
curl -s -m 2 "some_url" 2>&1 | grep "hello"
sleep 15m
done
因此,如果是在下午3点之后,那么开始进行curl调用直到上午8点,如果curl调用成功给出"hello"字符串,则成功退出,否则在上午8点后退出并显示错误信息。
如果是在下午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