1. 脚本编程之:编写HTTP服务脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
#!/bin/bash # # chkconfig: 35 75 25 # description: Apache is a World Wide Web server. It is used to serve \ # ----------------------------------------------------------------------- # Auth: # Email: # Version: # Create date: #------------------------------------------------------------------------ if [ -f /etc/sysconfig/httpd ]; then . /etc/sysconfig/httpd fi #-------------------------The color display------------------------# COLUMENS=80 SPACE_COL=$[ $COLUMENS-21 ] RED='\033[1;5;31m' GREEN='\033[1;32m' YELLOW='\033[33m' NORMAL='\033[0m' success() { REAL_SPACE=$[ $SPACE_COL - ${#1} ] echo -n "$1" for i in `seq 1 $REAL_SPACE`; do echo -n " " done echo -e "[ ${GREEN}0k${NORMAL} ]" } failure() { REAL_SPACE=$[ $SPACE_COL - ${#1} ] echo -n "$1" for i in `seq 1 $REAL_SPACE`; do echo -n " " done echo -e "[ ${RED}FAIL${NORMAL} ]" } #---------------------------Functions--------------------------------# LOCKFILE="/var/lock/subsys/httpd" PID=`ps aux | grep httpd | grep -v grep | grep root | grep /usr/local/httpd/bin/httpd$ | awk '{print $2}'` HTTPD="httpd" start() { /usr/local/httpd/bin/httpd [ $? -eq 0 ] && touch $LOCKFILE sleep 1 success "$HTTPD is starting.." } stop() { rm -fr $LOCKFILE kill $PID if [ ! $? -eq 0 ]; then failure "$HTTPD is stop" exit 1 fi sleep 1 success "$HTTPD is stopping.." } status() { if [ -e $LOCKFILE ]; then echo "$HTTPD is running [PID: $PID].." else echo "$HTTPD is stop" fi } #------------------------------Call----------------------------------# case $1 in start) if [ -e $LOCKFILE ]; then failure "$HTTPD already star" else sleep 1 start fi ;; stop) if [ ! -e $LOCKFILE ]; then failure "$HTTPD already stop" else sleep 1 stop fi ;; status) if [ -e $LOCKFILE ]; then echo -e "${GREEN}$HTTPD is running..PID:$PID${NORMAL}" else echo -e "\033[1;31m$HTTPD already stop${NORMAL}" fi ;; restart) if [ -e $LOCKFILE ]; then stop sleep 1 start else sleep 1 start fi ;; *) echo -e "\033[31;1mUsage:/etc/rc.d/init.d/$HTTPD {start|stop|restart|}\033[0m" esac |
PS:在Redhat系统中,服务脚本都是调用/etc/rc.d/init.d/functions这个函数文件,这个函数文件中定义有failure/success函数可以调用,并且接收输入参数。比你自己写的输出颜色要好一些,可以打开看看里面的函数,看看怎么调用。
2. 脚本编程之:调用functions函数文件编写Postfix服务脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
#!/bin/bash # # chkconfig: 2345 80 30 # description: Postfix is a Mail Transport Agent, which is the program \ # processname: master # pidfile: /var/spool/postfix/pid/master.pid # config: /etc/postfix/main.cf # config: /etc/postfix/master.cf # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ $NETWORKING = "no" ] && exit 3 [ -x /usr/sbin/postfix ] || exit 4 [ -d /etc/postfix ] || exit 5 [ -d /var/spool/postfix ] || exit 6 RETVAL=0 prog="postfix" start() { # Start daemons. echo -n $"Starting postfix: " /usr/bin/newaliases >/dev/null 2>&1 /usr/sbin/postfix start 2>/dev/null 1>&2 && success || failure $"$prog start" RETVAL=$? [ $RETVAL -eq 0 ] && touch /var/lock/subsys/postfix echo return $RETVAL } stop() { # Stop daemons. echo -n $"Shutting down postfix: " /usr/sbin/postfix stop 2>/dev/null 1>&2 && success || failure $"$prog stop" RETVAL=$? [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/postfix echo return $RETVAL } reload() { echo -n $"Reloading postfix: " /usr/sbin/postfix reload 2>/dev/null 1>&2 && success || failure $"$prog reload" RETVAL=$? echo return $RETVAL } abort() { /usr/sbin/postfix abort 2>/dev/null 1>&2 && success || failure $"$prog abort" return $? } flush() { /usr/sbin/postfix flush 2>/dev/null 1>&2 && success || failure $"$prog flush" return $? } check() { /usr/sbin/postfix check 2>/dev/null 1>&2 && success || failure $"$prog check" return $? } restart() { stop start } # See how we were called. case "$1" in start) start ;; stop) stop ;; restart) stop start ;; reload) reload ;; abort) abort ;; flush) flush ;; check) check ;; status) status master ;; condrestart) [ -f /var/lock/subsys/postfix ] && restart || : ;; *) echo $"Usage: $0 {start|stop|restart|reload|abort|flush|check|status|condrestart}" exit 1 esac exit $? # END |
3. 脚本编程之:批量添加或删除用户
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
#!/bin/bash # #./SCRIPTNAME.sh -v -h --add/--del peng,dong,wen declare -i DEBUG=0 declare -i ADD=0 declare -i DEL=0 if [ $# -lt 1 ]; then echo "Usage:./`basename $0` --add/--del USER,USER -v|--verbose -h|--help" exit 1 fi for i in `seq 1 $#`; do case $1 in -v|--verbose) DEBUG=1 shift 1 ;; -h|--help) echo "Usage:./`basename $0` --add/--del USER,USER -v|--verbose -h|--help" exit 0 ;; --add) ADD=1 ADDUSER=$2 shift 2 ;; --del) DEL=1 DELUSER=$2 shift 2 ;; *) echo "Usage:./`basename $0` --add/--del USER,USER -v|--verbose -h|--help" esac done if [ $ADD -eq 1 ]; then for USER in `echo $ADDUSER | sed 's@,@ @g'`; do if id $USER &> /dev/null; then [ $DEBUG -eq 1 ] && echo "user $USER exist." else useradd $USER echo "$USER" passwd --tdin $USER &> /dev/null [ $DEBUG = 1 ] && echo "user $USER add success." fi done fi if [ $DEL -eq 1 ]; then for USER in `echo $DELUSER | sed 's@,@ @g'`; do if id $USER &> /dev/null; then userdel -r $USER [ $DEBUG -eq 1 ] && echo "user $USER delete success." else [ $DEBUG = 1 ] && echo "user $SEER not exist." fi done fi |
4. 一个批量恢复游戏角色的脚本(仅供参考脚本逻辑关系的定义,不可使用)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
#!/bin/bash # if [ ! $# -ge 1 ];then echo -e "脚本使用方法:\033[1;32msh $0 role_id_1 role_id_2 ...\033[0m" exit 1 fi ###############定义变量############### red="\033[5;31m" red1="\033[1;31m" green="\033[1;32m" close="\033[0m" date=`date +%Y%m%d` db="storage" datapath="/root/dol_db_update" ###############定义函数############### move() { if [ -d $datapath/$date ];then echo -e "$green[$date]目录已经存在!$close" mv $datapath/$1 $datapath/$date &> /dev/null if [ $? = 0 ];then echo -e "$green[$1]文件移动到[$date]目录成功!$close" else if [ -e $datapath/$date/$1 ];then echo -e "$red[$1]$close已存在于$red[$date]$close目录中!" else echo -e "移动$red[$1]$close文件到$red[$date]$close目录失败,原因自行查看!" fi fi else cd $datapath mkdir $date echo -e "$green[$date]目录创建成功!$close" mv $datapath/$1 $datapath/$date &> /dev/null if [ $? = 0 ];then echo -e "$green[$1]文件移动到[$date]目录成功!$close" else if [ -e $datapath/$date/$1 ];then echo -e "$red[$1]$close已存在于$red[$date]$close目录中!" else echo -e "移动$red[$1]$close文件到$red[$date]$close目录失败,原因自行查看!" fi fi fi } del() { cd $datapath if [ -n $roleid ];then ./dol_db_update CHARA_DEL $roleid &> /dev/null echo -e "$green删除角色[$roleid]成功,自动重新恢复[$1]角色,请等待!$close" restore $replace $replace1 else echo -e "$red1删除角色[$roleid]失败,请检查你输入的角色ID是否正确!$close" echo -e "$red1角色恢复到[$1]终止,请重新运行脚本并输入需要恢复的角色ID号!$close" exit 1 fi } restore() { cd $datapath ./dol_db_update NECRO $1 > $1 grep "SUCCESS" $1 &> /dev/null if [ $? = 0 ];then echo -e "$green[$1]恢复成功$close!" mysql -e "SELECT id,name,account_id,created,last_save,lv_adv,lv_trd,lv_btl FROM $db.dol_chara_store WHERE id=$1;" move $replace echo -e "$green<<<===============================================角色ID[$1]恢复成功=================================================>>>$close" else echo -e "$red1[$1]恢复失败,失败原因如下!$close" grep "UNIQUE" $1 &> /dev/null if [ $? = 0 ];then echo -e "$red[$1]$close的角色名有重复,需要删除重复角色名才可以恢复$red[$1]$close,[$!]角色名如下:" mysql -e "SELECT id,name,account_id FROM $db.dol_chara_graveyard WHERE id=$1;" echo -e "$green重复角色如下,查询需要一点时间,请耐心等待......$close" ACC1=`mysql -N -s -e "select id from $db.dol_chara_store where name='$rolename';"` mysql -e "select id,name,account_id,created,last_save,lv_adv,lv_trd,lv_btl from $db.dol_chara_store where name='$rolename';" if [ -n "$ACC1" ];then read -p "输入需要删除的角色ID[$ACC1],你也可以输入[q|Q]退出脚本:" roleid case $roleid in q|Q) echo -e "$green角色恢复到[$1]结束,请重新运行脚本并输入需要恢复的角色ID号!$close" exit 1 ;; $ACC1) del $replace ;; *) read -p "输入的角色ID有误,请重新输入:" roleid esac fi else grep "slot_full" $1 &> /dev/null if [ $? = 0 ]; then echo -e "$red[$1]$close的账号下角色以满,需要删除一个角色后才可恢复$red[$1]$close!" mysql -e "SELECT id,name,account_id FROM $db.dol_chara_graveyard WHERE id=$1;" echo -e "$green[$1]账号角色如下,查询需要一点时间,请耐心等待:$close" ACC2=`mysql -N -s -e "select id from $db.dol_chara_store where account_id=$account_id;"` ACC3=$(echo $ACC2 | awk '{print $1}') ACC4=$(echo $ACC2 | awk '{print $2}') ACC5=$(echo $ACC2 | awk '{print $3}') mysql -e "select id,name,account_id,created,last_save,lv_adv,lv_trd,lv_btl from $db.dol_chara_store where account_id=$account_id;" if [ -n "$ACC5" ];then if [ -n "$ACC4" ];then read -p "从上面显示的角色中选择一个角色ID进行删除[$ACC3 $ACC4 $ACC5],你也可以输入[q|Q]退出脚本或敲回车继续恢复下一个ID[$2]:" roleid case $roleid in q|Q) echo -e "$green角色恢复到[$1]结束,请重新运行脚本并输入需要恢复的角色ID号!$close" exit 1 ;; $ACC3|$ACC4|$ACC5) del $replace ;; *) echo -e "$red1输入的角色ID不是给定的ID号[$ACC3 $ACC4 $ACC5],所以角色删除失败,因此[$1]恢复失败,继续恢复下一个角色[$replace1]!$close" esac else read -p "系统检测到info表中数据未导入到store表中,是否执行导入操作[y/n]:" yn case $yn in y|Y) echo "正在执行info表中数据导入到store表中,请等待....." mysql -u root storage < /usr/local/etc/koei/chara_tbl_mng.sql &> /dev/null restore $replace $replace1 ;; n|N) echo -e "$green由于不执行info表中数据导入到store表中,因此程序被迫中断,必须进行导入操作才可恢复角色或者跳过此角色重新运行角色!$close" exit 1 ;; *) echo -e "$green由于不执行info表中数据导入到store表中,因此程序被迫中断,必须进行导入操作才可恢复角色或者跳过此角色重新运行角色!$close" exit 1 esac fi else if [ -n "$ACC4" ];then read -p "从上面显示的角色中选择一个角色ID进行删除[$ACC3 $ACC4],你也可以输入[q|Q]退出脚本或敲回车继续恢复下一个ID[$2]:" roleid case $roleid in q|Q) echo -e "$green角色恢复到[$1]结束,请重新运行脚本并输入需要恢复的角色ID号!$close" exit 1 ;; $ACC3|$ACC4) del $replace ;; *) echo -e "$red1输入的角色ID不是给定的ID号[$ACC3 $ACC4],所以角色删除失败,因此[$1]恢复失败,继续恢复下一个角色[$replace1]!$close" esac else read -p "系统检测到info表中数据未导入到store表中,是否执行导入操作[y/n]:" yn case $yn in y|Y) echo "正在执行info表中数据导入到store表中,请等待....." mysql -u root storage < /usr/local/etc/koei/chara_tbl_mng.sql &> /dev/null restore $replace $replace1 ;; n|N) echo -e "$green由于不执行info表中数据导入到store表中,因此程序被迫中断,必须进行导入操作才可恢复角色或者跳过此角色重新运行角色!$close" exit 1 ;; *) echo -e "$green由于不执行info表中数据导入到store表中,因此程序被迫中断,必须进行导入操作才可恢复角色或者跳过此角色重新运行角色!$close" exit 1 esac fi fi else echo "$red1恢复角色ID[$1]出现故障,请根据错误日志信息逐一排查解决!$close,继续恢复下一个[$replace1]" cat $datapath/$1 fi fi fi } ###############检查输入合法性############### LIST=$@ for i in $LIST;do test=$(echo $i | sed 's/[0-9]//g') if [ ! -z $test ];then echo -e "$green对不起,你输入的角色ID有误,只能输入数字且数字字符个数不得大于10,请重新输入!$close" exit 1 else count=`echo ${#i}` if [ $count -gt 10 ];then echo -e "$green对不起,你输入的角色ID有误,只能输入数字且数字字符个数不得大于10,请重新输入!$close" exit 1 fi fi done ################业务逻辑################### for((i=0;$#>i;));do account_id=`mysql -N -s -e "SELECT account_id FROM $db.dol_chara_graveyard WHERE id=$1;"` rolename=`mysql -N -s -e "SELECT name FROM $db.dol_chara_graveyard WHERE id=$1;"` role_id=`mysql -N -s -e "SELECT id FROM $db.dol_chara_store WHERE id=$1;"` replace="$1" replace1="$2" if [ -s $account_id ]; then echo -e "$green没有查询到被删除角色ID[$1]!$close" if [ -n "$role_id" ];then echo -e "$green[$1]角色存在于dol_chara_store表中,表示此角色并没有被删除或已经恢复!$close" else echo -e "$green并且此角色ID[$1]不存在于dol_chara_store表中,由以上两个条件判断出,你输入的角色ID[$1]很有可能是一个错误的角色ID号,请检查后再次输>入...$close" fi else restore $replace $replace1 fi shift 1 done echo -e "\n" echo -e "$green<<<======================================时间[$date]共恢复角色如下==========================================>>>$close" cat $datapath/$date/* echo -e "$green<<<==================================================结束======================================================>>>$close" echo -e "\n" |