写一个给电脑设置北京时间的脚本,需要传入参数为sudo密码
set_now_time()
{
password=$1
#获取网页上北京时间
time_now=`curl -i https://biaozhunshijian.bmcx.com | grep date:`
daytime=`echo $time_now | awk '{print $6}'`
month=`echo $time_now | awk '{print $4}'`
year=`echo $time_now | awk '{print $5}'`
day=`echo $time_now | awk '{print $3}'`
hour=${daytime%%:*}
ms=${daytime%:*}
minitue=${ms##*:}
second=${daytime##*:}
echo ${hour}:${minitue}:${second}
case $month in
JAN|Jan|jan)
month=1
;;
FEB|Feb|feb)
month=2
;;
MAR|Mar|mar)
month=3
;;
APR|Apr|apr)
month=4
;;
MAY|May|may)
month=5
;;
JUN|Jun|jun)
month=6
;;
JUL|Jul|jul)
month=7
;;
AUG|Aug|aug)
month=8
;;
SEP|Sep|sep)
month=9
;;
OCT|Oct|oct)
month=10
;;
NOV|Nov|nov)
month=11
;;
DEC|Dec|dec)
month=12
;;
esac
#偏移8个小时
if [ "$hour" = "08" ]; then
hour=8
elif [ "$hour" = "09" ]; then
hour=9
fi
hour=$[hour+8]
if [ $hour -ge 24 ]; then
hour=$[hour-24]
day=$[day+1]
if [ $month -eq 1 ] || [ $month -eq 3 ] || [ $month -eq 5 ] || [ $month -eq 7 ] || [ $month -eq 8 ]|| [ $month -eq 10 ]|| [ $month -eq 12 ]; then
if [ day -gt 31 ]; then
day=$[day-31]
month=$[month+1]
if [ $month -gt 12 ]; then
month=$[month-12]
year=$[year+1]
fi
fi
elif [ $month -eq 4 ] || [ $month -eq 6 ] || [ $month -eq 9 ] || [ $month -eq 11 ];then
if [ month -gt 30 ]; then
day=$[day-30]
month=$[month+1]
fi
elif [ $month -eq 2 ]; then
if [ $[year%400] -eq 0 ]; then
if [ $day -gt 29 ]; then
day=$[day-29]
month=$[month+1]
fi
elif [ $[year%4] -eq 0 ] && [ $[year%400] -ne 0 ]; then
if [ $day -gt 29 ]; then
day=$[day-29]
month=$[month+1]
fi
else
if [ $day -gt 28 ]; then
day=$[day-28]
month=$[month+1]
fi
fi
fi
fi
if [ $day -lt 10 ]; then
day=$[day+0]
day=0$day
fi
if [ $month -lt 10 ]; then
month=$[month+0]
month=0$month
fi
echo "${year}-${month}-${day} ${hour}:${minitue}:${second}"
echo $password | sudo -S apt-get update
sudo date ${month}${day}${hour}${minitue}$[year-2000].${second}
}
#sudo登录密码为输入参数
set_now_time 1234