#!/bin/bash#--------------------------------------------# name: change-hosts.sh #--------------------------------------------#--------------------------------------------#Program:## 从laod博客更新hosts文件##--------------------------------------------#--------------------------------------------#History:# 2017/08/20 qi 1.0 #--------------------------------------------#--------------------------------------------PATH=/bin:/sbin:/usr/bin:/usr/sbin:usr/local/bin:usr/local/sbin:~/binexport PATHDATE=`date +%Y%m%d`DATEFULL=`date`URL="https://iiio.io/download/${DATE}/Android安卓跟Linux系列.zip"LOGPATH=/home/qi/logDOWNPATH=/home/qi/download/hostsPASSWD=("blog" "laod" "google")echo ${PASSWD[*]}#下载hosts文件到download文件夹;如果已经存在,将不会下载if test -e ${DOWNPATH}/Android安卓跟Linux系列.zip then echo "The zip is exists"else echo "Downloading ......" wget ${URL} -P ${DOWNPATH} || { echo "${DATEFULL} ${URL}:[URL error] " >> ${LOGPATH}/hosts.log exit 1}fi#判断下载的文件是否正确,若正确,解压缩;否则计入日志。if test -e ${DOWNPATH}/Android安卓跟Linux系列.zip then mkdir ${DOWNPATH}/${DATE} echo "Unzip doing ......" for pass in ${PASSWD[*]} do echo $pass unzip -P ${pass} -d ${DOWNPATH}/${DATE}/ ${DOWNPATH}/Android安卓跟Linux系列.zip # code 82 : password not correct if [ -f ${DOWNPATH}/${DATE}/hosts ] then echo -e "\n Correct ZIP passwd : ${pass}" echo "${DATEFULL} Unzip password is correct:[Unzip right] passwd:${pass}" >> ${LOGPATH}/hosts.log echo "Unzip done!!!" else echo -e "\nUnzip password error" echo "${DATEFULL} Unzip password not correct:[Unzip error] wrong pass:${pass}" >> ${LOGPATH}/host.log fi done echo echo "Coping ......" sudo cp -f ${DOWNPATH}/${DATE}/hosts /etc/ || { echo "${DATEFULL} hosts file can not be copied successfully!!!" >> ${LOGPATH}/hosts.log exit 2 } echo echo "Copy hosts to /etc success!!!" echo rm -rf ${DOWNPATH}/*.zip echo "Clean Over !!!" exit 0else echo "${DATE} hosts file not exists!!!" >> ${LOGPATH}/hosts.log exit 3fi