关于shell脚本函数、数组、字符串截取、svn更新发布实例

利用shell命令取本机ip地址

 cat /etc/sysconfig/network-scripts/ifcfg-eth0 | grep IPADDR | awk -F '=' '{print $2}'

 

利用shell脚本数组,定义一个模板文件,脚本里面引用这些变量,使用sed -i 替换,生成区配置文件

[root@localhost conf2]# cat xxx
#  server_name           server_id      server_domain         static_url       upload_url           ftp_ip       ip       socket_server       socket_port     chat_port      mysql_port     redis_port1     redis_port2   extra_ip
CONF=(官方:xxx    150134      xxx   xxx  xxx  xxx xxx    xxx  xxx     36134    xxx   3306     6379         6380     "" )

 

#/bin/bash  

#功能:QA服根据模板创建区配置文件并提交到svn上。

SOURCE_PATH=/data/source_code

SVN_PATH=/code/psm   #svn发布目录,要先推送到这个目录,然后更新提交

dir="/data/source_code/configfiles"

default_conf="config.properties"

default_socket_conf="socket.lp"

LOG_FILE='/tmp/log.log'

usage(){

                            cat <<EOF

                      echo_error 使用说明:

                 sh $0 username create_one_zone_config           #xxx

                 sh $0 username delete_one_zone_config           #xxx

EOF

    exit 1

}

 

if [ $# -eq 0 ] ; then

                 usage

fi

 

change_config_file(){

                 #根据/data/source_code/configfiles/conf目录下各分区配置信息生成区配置文件

                      ZONE_FILE="$1"

                      yunyingshang=`echo ${ZONE_FILE}|awk -F'_' '{print $2}'`

                      source ${dir}/conf/${ZONE_FILE}  #引用模板文件

                      [ ! -d ${SOURCE_PATH}/zonefile/${yunyingshang}/${ZONE_FILE}/bin/boot ] && mkdir -p ${SOURCE_PATH}/zonefile/${yunyingshang}/${ZONE_FILE}/bin/boot

                 ####生成和修改区配置文件

                            cp -rf ${dir}/${default_conf} ${SOURCE_PATH}/zonefile/${yunyingshang}/${ZONE_FILE}/bin/

                            cp -rf ${dir}/${default_socket_conf} ${SOURCE_PATH}/zonefile/${yunyingshang}/${ZONE_FILE}/bin/boot/

                            sed -i  -e "s#game.serverId=.*#game.serverId=${QA_CONF[0]}#" \

                                       -e "s#game.serverName=.*#game.serverName=${QA_CONF[1]}#" \

                                       -e "s#game.serverIp=.*#game.serverIp=${QA_CONF[2]}#" \

                                       -e "s#game.loginDomain=.*#game.loginDomain=${QA_CONF[3]}#" \

                                       -e "s#redis.host=.*#redis.host=${QA_CONF[6]}#" \

                                       -e "s#mysql.host=.*#mysql.host=${QA_CONF[8]}#" \     ####使用数组功能。

                                       ${SOURCE_PATH}/zonefile/${yunyingshang}/${ZONE_FILE}/bin/${default_conf}                                     

                            sed -i "s#port=.*#port=${QA_CONF[9]}#" ${SOURCE_PATH}/zonefile/${yunyingshang}/${ZONE_FILE}/bin/boot/${default_socket_conf}  

                                       echo "one configuration file has been generated" 

          

                 ###从生成目录同步区配置到SVN发布目录。                

                            rsync -avz --exclude=".svn"  ${SOURCE_PATH}/zonefile/${yunyingshang}/* ${SVN_PATH}/zonefile/${yunyingshang}/ >> ${LOG_FILE}

                 ###区文件推到GM目录。

                            rsync -avz --exclude=".svn"  ${SOURCE_PATH}/zonefile/${yunyingshang}/${ZONE_FILE}/bin/${default_conf}  ${SVN_PATH}/psm_gm/zq/config/subserver/${QA_CONF[0]}.properties >> ${LOG_FILE} 

}

change_one_zone_config_file(){

           change_config_file "${ZONE_NAME}"

}

delete_one_zone_config_file(){

           delete_config_file "${ZONE_NAME}"

}

 

delete_config_file(){

                 ZONE_FILE="$1"

                 yunyingshang=`echo ${ZONE_FILE}|awk -F'_' '{print $2}'`

                 source ${dir}/conf/${ZONE_FILE}  

                      rm -rf ${dir}/conf/${ZONE_FILE}

                      rm -rf ${SOURCE_PATH}/zonefile/${yunyingshang}/${ZONE_FILE}

                      rm -rf ${SVN_PATH}/zonefile/${yunyingshang}/${ZONE_FILE}

                      rm -rf ${SVN_PATH}/psm_gm/zq/config/subserver/${QA_CONF[0]}.properties

                      echo "delete_one_zone_config is OK!"

}

 

 

update()

{

SVN_NAME=$1

      svn update --username ${SVN_NAME}

      if [ $? -ne 0 ];then

           echo  "svn update failed"

           exit 1

      fi

      VERSION=`svn info |awk -F"[ ]+" '/Revision/{print $2}'`

      TEMP="`date +%Y%m%d`${VERSION}"

      NUM=`echo ${TEMP}+1|bc`

      echo -e "####################\nsvn update complete"

}

 

commit ()

{

LOG_PATH="/code/svnlog"

      #svn commit

      echo "cd ${1:-${SVN_PATH}}"

      cd ${1:-${SVN_PATH}}

      [ ! -d ${LOG_PATH} ] && mkdir -p ${LOG_PATH}

      mkdir -p ${LOG_PATH}/${VERSION}

      $SVN status |awk '{if($1=="!") print $0}' |cut -b 9- >${LOG_PATH}/${VERSION}/svn_del.log

      $SVN status |awk '{if($1=="?") print $0}' |cut -b 9- >${LOG_PATH}/${VERSION}/svn_add.log

      $SVN status |awk '{if($1=="M") print $0}' |cut -b 9- >${LOG_PATH}/${VERSION}/svn_change.log

      while read line ;do $SVN delete "$line" ;done <${LOG_PATH}/${VERSION}/svn_del.log  && echo "svn delete complete"

      while read line ;do $SVN add "$line" ;done <${LOG_PATH}/${VERSION}/svn_add.log   &&  echo "svn add complete"

      $SVN commit -m "`date +%Y%m%d%H%M%S`" --username ${SVN_NAME} && echo "svn commit complete"

      #删除认证文件

      rm -f /root/.subversion/auth/svn.simple/*

}

 

if [ -n "$2" ];then

      SVN_NAME="$1"

           case $2 in

           create_one_zone_config)

                 if [ -n "$3" ];then

                 change_one_zone_config_file

                else

                        echo -e "Please input zone name parameter for change_one_zone_config_file! For example: \c" && echo -e "\033[40;32;1m create_one_zone_config xxx \c" && echo -e "\033[0m!"

                        exit 1

                fi

                      update $1

                      commit 

                      ;;

           delete_one_zone_config)

            if [ -n "$3" ];then

                        delete_one_zone_config_file

                else

                        echo -e "Please input zone name parameter for delete_one_zone_config_file! For example: \c" && echo -e "\033[40;32;1m delete_one_zone_config psm_9wee_s0 \c" && echo -e "\033[0m!"

                        exit 1

                fi

               update $1

               commit

                ;;

           *)

                      usage

                      ;;

           esac

fi

 

posted @ 2017-07-21 14:15  GeminiMp  阅读(670)  评论(0编辑  收藏  举报