In a bash script (run every 30min), I want to do an action every even hour (and minutes 0), so I want to test the hour number if it's an even number or not. So I use this:
HEURE=$(date +"%H")
MINUTES=$(date +"%M")
HEURE_PAIRE=""
MINUTES_ZERO=""
[[ $((HEURE % 2)) -eq 0 ]] && HEURE_PAIRE="OUI" || HEURE_PAIRE="NON"
[[ $MINUTES -eq 0 ]] && MINUTES_ZERO="OUI" || MINUTES_ZERO="NON"
if [[ "${HEURE_PAIRE}" == "OUI" ]] && [[ "MINUTES_ZERO" == "OUI" ]]; then
# My code
fi
But I get an error for this line (n°170 in my script) [[ $((HEURE % 2)) -eq 0 ]] && HEURE_PAIRE="OUI" || HEURE_PAIRE="NON"
when it was 08:00
, 08:30
, 09:00
, 09:30
, but not for 10:00
or 10:30
. See the error I get:
/volume4/docker/_Scripts-DOCKER/driver-pkgctl-r8152-restart-reload.sh: line 170: 08: value too great for base (error token is "07")
/volume4/docker/_Scripts-DOCKER/driver-pkgctl-r8152-restart-reload.sh: line 170: 09: value too great for base (error token is "09")
I expected to have it working like this:
For 08:00
, the 08%2
should return 0
as a result, proving hour is even.
For 09:00
, the 09%2
should return 1
as a result, proving hour isn't even.
Not having an error
I assume it's the 0
before the 8
or 9
... But I don't know how to fix this.
I found a workaround with this question by adding this:
HEURE=${HEURE#0}
It seems to be working.
My question: is there a better way to achieve what I want?
Thanks in advance.