0

I have a script that looks at the current time, does some tasks and notifies me 1 minute before the timer ends. For some reason there is a problem and the script does not pass the condition "IF" I read that it seems like bash interprets numbers not as a decimal number. I tried to solve the problem by removing the first zero from the string. Bash will understand this as a decimal number and perform a subtraction operation.

For example. Turn off the computer after 10 minutes. Now is 01:00:00. Shutdown at 01:10:00 The script makes a timecode file with name "T01H10M.shutdown" and takes all the necessary numbers of hour and minute from it. When the time comes to 01:09:00, the script notifies me about the imminent completion of the work.

#Search shutdown time from file name
            SearchFilenameDateWhenPCgotoShutdown="$(find /root/ -name '*.shutdown')"
            SearchTimeHour=$(echo $SearchFilenameDateWhenPCgotoShutdown | cut -f2 -d "T"| cut -f1 -d "H")
            SearchTimeMinute=$(echo $SearchFilenameDateWhenPCgotoShutdown | cut -f2 -d "H" | cut -f1 -d "M")

#Calculation of the penultimate minute and сorrection of the subtraction error.
            SearchTimeMinuteMinus1=$(($SearchTimeMinute - 1 ))
            if [ $SearchTimeMinuteMinus1 -eq -2 ]; then
                SearchTimeMinuteMinus1=58;
            fi
            if [ $SearchTimeMinuteMinus1 -eq -1 ]; then
                SearchTimeMinuteMinus1=59
            fi
    
    #Determining the current hour and minute
            RealTimeHour=$(date "+%H")
            RealTimeMinute=$(date "+%M")
            if [ $RealTimeMinute = 00 ];
                then
                RealTimeMinute=0;
                else
                RealTimeMinute=`echo $RealTimeMinute |sed 's/^0*//'`
            fi

    #The condition matches the current hour
    if [ $SearchTimeHour = $RealTimeHour ]; then
        if [ $SearchTimeMinuteMinus1 = $RealTimeMinute ]; then
            curl -s "https://api.telegram.org/bla bla bla bla &> /dev/null
        fi

And here there is an error that when the current time is in minutes from 00-07 minutes, the condition IF = 00 then = 0 else remove "0" work fine and corrects the number to decimal with a condition. Output 1, 2, 3...7 And when the minute is already 08, the script stops at the stage of checking minutes.

if [ $SearchTimeMinuteMinus1 = $RealTimeMinute ]; then

And I don't understand why!

I tried to do it in a separate script.

B=08
if [ $B = 00 ];
B=0;
else
B=`echo $B |sed 's/^0*//'`
fi
echo $B

He works. Output 8. Why I can't do it in the main script, I don't understand.

Screenshot with each 2 min script run

Help please.

--------------------UPD 4.10.22--------------------

OK! Thank you all for your advice. I fixed the code and it works. But! Another problem :D When the time comes at 59 minutes. An error occurs in stage.

if [ $SearchTimeHour = $RealTimeHour ]; then

Problem

This is very strange! After all, all variables are defined correctly. Any ideas?

Full code:

#!/bin/bash

if ping -c 2 100.100.100.100 | grep "ttl"; then
    if ping -c 2 192.168.10.1 | grep "ttl"; then
        if ping -c 2 192.168.88.102 | grep "ttl"; then
    #OMV name
    NameOMV="Standart OMV"

    #The option determines how many minutes to turn off the system
    ShutDownParameterMinutes=2

    #Defining the startmark file in the script
    startmark=/root/startmark
    #Checking the existence of the startmark file
    if [ -f $startmark ]
    then
        #Recording when to switch off to a variable
        SearchFilenameDateWhenPCgotoShutdown="$(find /root/ -name '*.shutdown')"
        
        #Determination of the hour and minute of shutdown
        SearchTimeHour=$(echo "$SearchFilenameDateWhenPCgotoShutdown" | cut -f2 -d "T"| cut -f1 -d "H")
        SearchTimeMinute=$(echo "$SearchFilenameDateWhenPCgotoShutdown" | cut -f2 -d "H" | cut -f1 -d "M")
        
        #Determination of the hour and minute of shutdown minus 1 minute for notification in telegrams
        #Correction of the subtraction error of 2 minutes - 2 minutes becomes 58 minutes, and -1 = 59 minutes
        SearchTimeMinuteMinus1=$((SearchTimeMinute - 1 ))
        if [ "$SearchTimeMinuteMinus1" -eq -2 ]; then
            SearchTimeMinuteMinus1=58
        fi
        if [ "$SearchTimeMinuteMinus1" -eq -1 ]; then
            SearchTimeMinuteMinus1=59
        fi
curl -s "https://api.telegram.org/blablabla&text=%F0%9F%93%B7 SearchTimeMinuteMinus1=${SearchTimeMinuteMinus1}" &> /dev/null
        
        #Determining the current hour and minute
        RealTimeHour=$(date "+%H")
        RealTimeMinute=$(date "+%M")
        RealTimeMinute=$((10#$RealTimeMinute))

curl -s "https://api.telegram.org/blablabla&text=%F0%9F%93%B7 RealTimeHour=${RealTimeHour}" &> /dev/null
curl -s "https://api.telegram.org/blablabla&text=%F0%9F%93%B7 RealTimeMinute=${RealTimeMinute}" &> /dev/null
curl -s "https://api.telegram.org/blablabla&text=%F0%9F%93%B7 TICK $(date "+%d.%m.%Y - %H:%M:%S")" &> /dev/null
        
        #The condition matches the current hour
        if [ "$SearchTimeHour" -eq "$RealTimeHour" ]; then
curl -s "https://api.telegram.org/blablabla&text=%F0%9F%93%B7 Hour OK" &> /dev/null
            if [ "$SearchTimeMinuteMinus1" -eq "$RealTimeMinute" ]; then
curl -s "https://api.telegram.org/blablabla&text=%F0%9F%93%B7 Minute OK" &> /dev/null
                rm "$SearchFilenameDateWhenPCgotoShutdown"
                rm startmark
                curl -s "https://api.telegram.org/blablabla&text=%F0%9F%93%B7 $NameOMV is less 1 minutes left before shutdown" &> /dev/null
            fi
        fi
    else
        #Creating a startmark
        touch startmark
        #Notification in telegram about the start of the shutdown procedure
        echo "OrangePi is creating startmark to offline: $(date "+%d.%m.%Y - %H:%M:%S")" >> shutdownlog.txt

        #We determine the time when to turn off
        DateWhenPCgotoShutdown=$(date +"T""%H""H""%M""M" --date="$ShutDownParameterMinutes minute")
        TimeStampStartName="${DateWhenPCgotoShutdown}.shutdown"
        touch "$TimeStampStartName"
        chmod +x "$TimeStampStartName"

        #Notification in telegram when to turn off the computer
        TelegramMessage="OrangePi $NameOMV is creating startmark to offline: $(date "+%d.%m.%Y - %H:%M:%S" --date="$ShutDownParameterMinutes minute")"
        echo "${TelegramMessage}" >> shutdownlog.txt
        curl -s "https://api.telegram.org/blablabla&text=%F0%9F%93%B7 $TelegramMessage" &> /dev/null
        #/sbin/shutdown -h +$ShutDownParameterMinutes
        echo "SHUTDOWN"
    fi
        fi
    fi
fi

With respect.

Mk51
  • 1
  • 1
  • 1
    To quote https://wooledge.org/~greybot/meta/august -- *August is the month when all your scripts break because you placed `$(date +%m)` in a variable and tried to do arithmetic with it, without removing the leading zeros. `08` is considered octal. Use `$((10#$month))` to force decimal, or strip the zero.* – Charles Duffy Oct 03 '22 at 23:06
  • Also, you've got a bunch of quoting bugs here -- run your code through http://shellcheck.net/ and fix what it finds. – Charles Duffy Oct 03 '22 at 23:06
  • Anyhow -- `=` is a string operation so it doesn't care, but `-eq` is arithmetic, so it tries to parse things as numbers and fails on `08`. If you used `-eq` in your separate test you would have seen the problem. Likewise, `$(($SearchTimeMinute - 1 ))` is also arithmetic. – Charles Duffy Oct 03 '22 at 23:08
  • BTW, think about using bash's built-in regex support instead of messing with `cut` to extract individual fields. See `[[ $string =~ $re ]]` and the `BASH_REMATCH` array. – Charles Duffy Oct 03 '22 at 23:09
  • Does ["Value too great for base (error token is "09")"](https://stackoverflow.com/questions/21049822/value-too-great-for-base-error-token-is-09) answer your question? – Gordon Davisson Oct 04 '22 at 00:08
  • Please see the changes in the message. Thanks. – Mk51 Oct 04 '22 at 13:23

0 Answers0