#!/bin/bash
crontab -l | grep . #看看crontab 是不是空的
notEmpty=$?
crontab -l | grep sntp #看看crontab 是不是含有 sntp
if [ $? -ne 0 ];then
/usr/bin/expect <<EOF
set timeout 5
spawn crontab -e
if { $notEmpty == 1 } { #$notEmpty与左边大括号必须要有空格 ,1 与右边大括号必须要有空格
send "i" #发送大写G
} else { # 必须与 if的结束 大括号 在同一行,最后 else 前面都有空格
send "G" #发送大写G
send "o" #发送小写o
}
send "*/2 * * * * sudo /usr/bin/sntp -sS 192.168.0.2 > /dev/null 2>&1" #192.168.0.2是时钟服务器
send "\x1b:wq\r" #这个是esc + :wq + 回车键
#send ";2R1;95;0c\r" #似乎第一次 运行 crontab 的时候有弹窗,我们输入这个东西,弹出就没有了,就相当于点弹窗的 "允许" 的意思吧,,,,但是这一行代码,加上去,似乎没有什么用
#expect "exit\r" #退出,感觉可有可无吧 #最好不要使用这一行
expect eof #不允许用户交互,直接退出(这个用的会比interact多)EOF
fi