I have started from scratch as you told me and indeed when I did the cmd i2cdetectl -y 7 I could see the RTC, however having modified the script in the way that I had thought and which is just the one you commented the message continues to appear:
line 48: i2cdetect: command not found
RTC not detected, exiting.
I am attaching the modified script in case you made any other changes and could let me know. Once again thanks for your help
#!/bin/bash
I2C_ADDR=“68”
RTC_REG=“0x00”
I2C_GET=“i2cget -y 7 0x$I2C_ADDR”
I2C_SET=“i2cset -y 7 0x$I2C_ADDR”
I2C_DETECT=“i2cdetect -y 7 0x$I2C_ADDR 0x$I2C_ADDR”
DATE_CHECK="^[12][0-9]{3}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01]) ([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$";
MYNAMEIS="$(basename $0)"
read_rtc () {
NEW=""; for NN in {0…6}; do NEW="$NEW $($I2C_GET $(($RTC_REG + $NN)) b)"; done
NEW="$(echo $NEW | awk ‘{printf “20%02X-%02X-%02X %02X:%02X:%02X”, $7, $6, $5, $3, $2, $1}’)"
echo “RTC time data: $NEW”
if [[ $NEW =~ $DATE_CHECK ]]; then
echo “Setting system time, please wait (10 sec.) …”
timedatectl set-ntp 0
sleep 5
timedatectl set-time “$NEW”
if [ $? -ne 0 ]; then echo “FAILED: timedatectl (Setting too often?)”; fi
sleep 5
timedatectl set-ntp 1
echo “Finished.”
else
echo “RTC time is not correct. (Maybe battery replaced?)”
echo “Try resetting RTC first by running: $MYNAMEIS reset-rtc”
echo “Nothing changed.”
fi
}
write_rtc () {
NEW="$(date +‘0x%02S 0x%02M 0x%02H 0x%02u 0x%02d 0x%02m 0x%02y’)"
echo “Sending data to RTC: $NEW”
echo " sec. min. hour wkday day mnth year"
$I2C_SET $RTC_REG $NEW i
if [ $? -ne 0 ]; then echo “FAILED: i2cset (Some epic fail?)”; fi
echo “Giving it a sec, then reading back …”
sleep 1
NEW=""; for NN in {0…6}; do NEW="$NEW $($I2C_GET $(($RTC_REG + $NN)) b)"; done
NEW="$(echo $NEW | awk ‘{printf “20%02X-%02X-%02X %02X:%02X:%02X”, $7, $6, $5, $3, $2, $1}’)"
echo “RTC time data: $NEW”
}
if [ -z "$($I2C_DETECT | grep " $I2C_ADDR “)” ]; then
echo “RTC not detected, exiting.”
exit 1
fi
if [[ $# -gt 0 ]]; then
if [ $1 == ‘reset-rtc’ ]; then
echo “Resetting RTC with current system time …”
write_rtc
exit 0
fi
fi
if [ -z “$(timedatectl status | sed -n ‘s/.System clock synchronized[^a-zA-Z](yes).*/\1/Ip’)” ]; then
echo “NTP time is NOT syncronised, getting RTC-time …”
read_rtc
else
echo “NTP time is syncronised, overwriting RTC-time …”
write_rtc
fi