脚本不执行

脚本不执行

Linux comp001 3.18.6-2-ARCH #1 PREEMPT Sun Feb 8 09:43:43 MST 2015 armv6l GNU/Linux我有 rPi B+ 运行带有实时时钟硬件内核的 ArchLinux实时时钟 Pi。但是,我想检查启动时是否有互联网连接并运行 ntp 客户端。如果 ntp 客户端连接到 ntp 服务器并获取时钟,我现在想要与硬件同步时钟。我只想在互联网连接(ntp 客户端检查返回代码)离线时使用硬件时钟。我已经构建了一个服务,运行正常,但我不知道如何检查 ntp 客户端是否启动没有任何问题。我该如何检查?好吧,我一直在研究以下带有更正空格的脚本,但仍然不起作用:

#!/bin/sh

if [ ?('modprobe -v rtc_ds1307') = 0 ]; then
    # rtc is connected and working, use it

    echo "RTC Hardware connected, getting time"

    systemctl disable ntpd.service
    systemctl stop ntpd.service

    echo "ds1307 0x68" > /sys/class/i2c-adapter/i2c-1/new_device
    hwclock -s
else
    # rtc is offline / not connected, get time via ntp

    echo "RTC Hardware disconnected/battery dead/offline, use NTP"

    systemctl enable ntpd.service
    systemctl start ntpd.service
fi

为什么脚本执行在前 3 行失败:

./script_rtc.sh: line 3: syntax error near unexpected token `'modprobe -v rtc_ds1307''
./script_rtc.sh: line 3: `if [?('modprobe -v rtc_ds1307')=0]; then'

答案1

我通过以下方式重写了 if 子句:

#!/bin/sh

modprobe -v rtc_ds1307

if [ $? -eq 0 ]; then
    # rtc is connected and working, use it

    echo "RTC Hardware connected, getting time"

    systemctl disable ntpd.service
    systemctl stop ntpd.service

    echo "ds1307 0x68" > /sys/class/i2c-adapter/i2c-1/new_device
    hwclock -s
else
    # rtc is offline / not connected, get time via ntp

    echo "RTC Hardware disconnected/battery dead/offline, use NTP"

    systemctl enable ntpd.service
    systemctl start ntpd.service
fi

现在一切正常了!

相关内容