T
T
Tech2020-10-06 12:15:13
linux
Tech, 2020-10-06 12:15:13

Criticize the bash script?

I am very far from scripts and development. It took some time to make a simple script. Check, pliz, who does not complicate. The script works. Threw it into fcron for execution once a minute. But sometimes messages in telegrams fall with a delay of 3 to 20 minutes.

#!/bin/bash
KEY="xxxxxxx:yyyyyyyyyyy" #ключ телегграм-бота
TARGET="-4343905834509345098345" #id группы телеграм
URL1="https://api.telegram.org/bot$KEY/sendMessage"
output=$(curl -s 'https://экзампл.ком/api/posts/json')
OLD_TIME=$(echo $(cat /root/telegram/previous_post_time.json)) #время создания предыдущего поста, записанного в epoch-time.
GET_TIME=$(echo $output | jq .posts[0].created) #вытаскиваем из запроса время создания последнего поста
if (( $GET_TIME > $OLD_TIME )); then
    TIME=$(echo $GET_TIME>/root/telegram/previous_post_time.json) #записываем новое значение времени создания поста.
    GET_URL=$(echo $output | jq -r .posts[0]._links[1].href)                               
    GET_USER=$(echo $output | jq -r .posts[0].user.login)   
    URL=$(echo $GET_URL>/root/telegram/url.json)                                                        
    TEXT="$GET_USER $GET_URL"
    PAYLOAD="chat_id=$TARGET&text=$TEXT&parse_mode=Markdown"
    curl -s --max-time 10 --retry 5 --retry-delay 2 --retry-max-time 10 -d "$PAYLOAD" $URL1 > /dev/null 2>&1 &
else
    exit 0
fi
exit 0

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Saboteur, 2020-10-06
@bioid

if (( $GET_TIME > $OLD_TIME )); then

Here is an arithmetic comparison.
Most likely, the block is executed with an error all the time, because it is not clear what is in $GET_TIME, and there is hardly a recognizable command. Unless something intelligible accidentally hits. If you give examples of what gets into these variables, it will be clearer.
You need to compare not dates, but timestamps

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question