Chinaunix首页 | 论坛 | 博客
  • 博客访问: 532607
  • 博文数量: 137
  • 博客积分: 3170
  • 博客等级: 中校
  • 技术积分: 1455
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-17 11:47
文章分类

全部博文(137)

文章存档

2015年(2)

2013年(1)

2012年(6)

2011年(5)

2010年(62)

2009年(61)

我的朋友

分类:

2010-03-13 20:35:58

1.连接字符串和for循环


for PORT in 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315
do
    thisport=/etc/mycnf/my$PORT.cnf
    /mysql/bin/mysqld_safe --defaults-file=$thisport --user=mysql &
    sleep 1
    echo mysql $PORT is listening!
done


2. ./mysqldataimport.sh: line 17: 在未预料的“done”附近出现语法错误,原来是在else处加入exit,结尾没有exit,以下代码不会出现上面的错误。以下脚本执行时间大概在1个小时,比较长。

#!/bin/bash
##jacky 2010/03/17 11:36:51
#read carefully before execute mysqldataimport.
DST_DB='192.168.0.88'
SQL_FILE='datadb.sql'
for PORT in 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315
do
    mysql -h$DST_DB -uroot -proot -P$PORT < $SQL_FILE
    if [ $? -eq 0 ]
    then
        echo mysql $PORT import data exit with $? success ! # exit with 0 represent success
    else
        echo $PORT
    fi
done
exit


3.遍历目录清空目录中的所有log文件,注意for dir in $1/* 遍历目录时的写法。

#!/bin/bash
for dir in $1/*
do
    for file in ${dir}/*
    do
        echo "$file"
        cat /dev/null > $file
    done
done  


4.if else elif用法

if [ $# == 0 ] ; then # no args 分号是命令分割符,使得if 和then可以在同一行
  nohup ./your${exe} &
  echo "Start
your$exe not in gdb Success"
elif [ $# == 1 ] ; then #1 args
  echo "Start
your$exe in gdb Success"
  gdb
your${exe}
else ; :  # >=2 args 冒号是空命令
  echo "Start exe excepet
your$exe Success"

5. shell数组的使用

#!/bin/bash
BAK_DATE=`date +%F-%R` # the time of bak operation

my_srv=( "1debug" "2debug" "3debug" "4debug" )

n_my_srv=${#my_srv[@]} # size of my_srv[]

for((i=0; i<$n_my_srv ;i++)) #walk through the my_srv
do
    tmp=$common_dir${my_srv[i]}/nohup.out
    bakedir_tmp=${bak_dir}
    if [ -e $tmp ]
    then
    tail -n 20000 $tmp > ${bakedir_tmp}${BAK_DATE}.out
    cat /dev/null > $tmp
    else
    :
    fi
done


3. mysql日志管理:
注意:如果此脚本放在crotab下面,在脚本中应该都使用绝对路径,否则会找不到。can't stat.

#!/bin/bash
##jacky 
DST_HOST='192.168.0.137'
MYSQL_LOG_DIR="path/to/mysql/log"

for PORT in 3309 3310
do
    #bin log
        mysql -uroot -proot -h$DST_HOST -P$PORT -e "PURGE BINARY LOGS BEFORE DATE_SUB( NOW( ), INTERVAL 7 DAY);"
    #go to log dir of PORT
    # cd $MYSQL_LOG_DIR/${PORT}log
    PORT_MYSQL_LOG_DIR=$MYSQL_LOG_DIR/${PORT}log
    #slowquery log
    mv -f $PORT_MYSQL_LOG_DIR/slowquery${PORT}.log $PORT_MYSQL_LOG_DIR/slowquery${PORT}-old.log
    #query log
    mv -f $PORT_MYSQL_LOG_DIR/${PORT}.log $PORT_MYSQL_LOG_DIR/${PORT}-old.log
    #error log
    mv -f $PORT_MYSQL_LOG_DIR/${PORT}err.log $PORT_MYSQL_LOG_DIR/${PORT}err-old.log
    /data/all_srv_db/mysql/bin/mysqladmin -uroot -proot -h$DST_HOST -P$PORT flush-logs
done
        echo flush mysql logs end
exit


4.利用awk过滤日志的脚本
使用方法:
./script_name YYYY/MM/DD/HH/MM(begin time)  YYYY/MM/DD/HH/MM(end time)

#!/bin/sh
gawk '
BEGIN {
FS= "[ \t:]+" #define the separator
ARGC = 2 #to cheat the awk ,避免报错提示!!
# extract log begin time
begin_year = substr( ARGV[2],1,4)
begin_month = substr(ARGV[2] ,6,2)
begin_day = substr( ARGV[2], 9,2)
begin_hour = substr( ARGV[2],12,2)
begin_minute = substr( ARGV[2],15 ,2)
#begin time
startlogtime = mktime(" "begin_year" "begin_month" "begin_day" "begin_hour" "begin_minute" 00 " )
print startlogtime
# extract log end time
end_year = substr( ARGV[3],1,4)
end_month = substr(ARGV[3] ,6,2)
end_day = substr( ARGV[3], 9,2)
end_hour = substr( ARGV[3],12,2)
end_minute = substr( ARGV[3],15 ,2)
endlogtime = mktime(" "end_year" "end_month" "end_day" "end_hour" "end_minute" 60 " )
print endlogtime

}

{
# change the month format to number
num_month = abbmonth2num( $2 )
timestamp = mktime(" "substr( $8,1,4)" "num_month" "$3" "$4" "$5" "$6" " )
                                                            #*********Edit**userid***Here************
if ( startlogtime < timestamp && timestamp < endlogtime && 0 != match( $0, /exp/ ) )
        print $0 > "userid.log"

}

END {
close("userid.log")
}

#abbmonth2num function def
#change abb month to its number
function abbmonth2num( abbstr_month )
{
    switch ( abbstr_month )
    {
      case "Jan":
          num_month = 1
          break
      case "Feb":
          num_month = 2
          break
      case "Mar":
          num_month = 3
          break
      case "Apr":
          num_month = 4
          break
      case "May":
          num_month = 5
          break
      case "Jun":
          num_month = 6
          break
      case "Jul":
          num_month = 7
          break
      case "Aug":
          num_month = 8
          break
      case "Sep":
          num_month = 9
          break
      case "Oct":
          num_month = 10
          break
      case "Nov":
          num_month = 11
          break
      case "Dec":
          num_month = 12
          break
      default:
          num_month = -1
          break
    }
    return num_month
} # end abbmonth2num function def

'
$*


阅读(891) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~