zoukankan      html  css  js  c++  java
  • 常用shell脚本

    1.批量改名或拷贝文件

    比如将 start*.sh文件改为stop*.sh

    拷贝

    先查看

    ls -l start*.sh|awk '{m=$9; gsub(/start/,"stop",$9);print "cp "m " "$9}'

    再运行

    ls -l start*.sh|awk '{m=$9; gsub(/start/,"stop",$9);print "cp "m " "$9}'|sh

    改名

    先查看

    ls -l start*.sh|awk '{m=$9; gsub(/start/,"stop",$9);print "mv "m " "$9}'

    再运行

    ls -l start*.sh|awk '{m=$9; gsub(/start/,"stop",$9);print "mv "m " "$9}'|sh

    2.改进grep 2048字符限制的小工具

    我们查找匹配文件时常常遇到grep报行长度超过2048这样的错误,我做了这个小工具,findtxt,可查找当前目录下的匹配文件,如果需要,把注释打开,可显示匹配内容。希望对大家有所帮助。

    for file in `find . ! -type d`

    do

    line=`sed -n /$1/p $file`

       if [ "$line" != "" ]; then

        echo $file

    #   echo $line

      fi

    done

    3.查看文件使用者的小工具

    对fuser的一点改进:

    1.vi showuser  建立一个文件,内容只有一句:

       find $1  -exec fuser -u {} ; 2>;&1|awk '{ if ($2 != ""  print $1 $2 }'

    2. chmod 755 showuser

    3. cp showuser /usr/bin

    4.  showuser  .

         showuser  /usr

    会把当前目录下的在使用的文件的文件名,使用者的id,name显示出来.

    4.清理垃圾,可放在crontab里每天执行。

    #rmlog.sh

    find /tmp ! -name "*.X11*" -mtime +7 -exec rm -f {} ;

    find /var/tmp -mtime +5 -exec rm -f {} ;

    find /var/preserve  /recycle -mtime +7 -exec rm -f {} ;

    crontab

    0 1 * * * /home/scripts/rmlog.sh  > /tmp/rmlog.log 2>&1

    5.强制关闭vg(包括umount所有相关文件系统)

    #varyoffvg_force.sh

    if [ $# -le 0  ]  ;then

    echo "no para, example:varyoff_vg.sh erpapp_vg "

    exit

    fi

    df -k|awk '{print $7 }'|grep -v Mounted >/tmp/fs_mounted.txt

    for i in `lsvg -l $1 |grep -vE "N/A|vg|MOUNT"|awk '{print $7}'`

    do

    if [ `grep -c $i /tmp/fs_mounted.txt`  -ge 1 ] ; then

      echo fuser -kc $i

      umount $i

    fi

    done

    varyoffvg $1

    6.kill_fs_user.sh

    (停掉使用某文件系统的用户,自动判断该文件系统是否mount,避免kill掉其他用户)

    if [ $# -le 0  ]  ;then

    echo "no para, example:kill_user.sh /applprod "

    exit

    fi

    df -k|awk '{print $7 }'|grep -v Mounted >/tmp/du_.txt

    if [ `grep -c $1  /tmp/du_.txt`  -eq 1 ] ; then

      echo fuser -kc $1

    fi

    7.mklvcopy做镜像

    相当于mirror rootvg,但当rootvg里有不想mirrror的lv或盘大小不一时比较有用。

    ##mkmirrorvg.sh

    mklvcopy -k hd5 2 $1

    mklvcopy -k hd6 2 $1

    mklvcopy -k hd8  2 $1

    mklvcopy -k hd4  2 $1

    mklvcopy -k hd9var  2 $1

    mklvcopy -k hd3   2 $1

    mklvcopy -k hd1   2 $1

    mklvcopy -k hd2   2 $1

    mklvcopy -k hd10opt  2 $1

    mklvcopy -k lg_dumplv  2 $1 

    bosboot -ad $1

    bootlist -m normal $1

    8.察看根目录各文件和子目录大小,去除文件系统统计

    # du_.sh

    df -k|awk '{print $7 }'|grep -v Mounted >/tmp/df_mounted.txt

    cd /

    for i in `ls -l|awk '{print $9}'|grep -v "-i"`

    do

    if [ `grep -c $i /tmp/df_mounted.txt`  -eq 0 ] ; then

      du -sk $i

    fi

    done

    9.防止文件系统下和根目录下rm -rf * 误操作

    #deny_rmall.sh

    cd /;touch ./-i;df -k|grep -v Mounted|grep -v proc|grep -v "/tmp"|awk '{print "cp "./-i"  " $7'}|sh

    cp "/-i" /etc

    cp "/-i"  /dev

    10. 保存清理errpt,(当然可以改为其他目录 )

    #errclear.sh

    errpt >/home/mxin/mon/log/errpt_`date +%Y%m%d`.log

    errpt -a >>/home/mxin/mon/log/errpt_`date +%Y%m%d`.log

    errclear 0

    11.起大量shell脚本

    #start_procs.sh

    cat start_procs.list|awk '{print "sh "$1".sh"}'|sh

    #start_procs.list(可追加修改)

    startprocessor

    ProcProcessor

    /home/scripts/startArocessor

    p_mj_deal_cardevent

    12.停大量进程

    # stop_procs.sh

    cat procs.list|awk '{print "stop_proc.sh   "$1}'|sh

    #stop_proc.sh

    ps -ef|grep $1|grep -v grep|awk '{print "kill -9 "$2}'|sh

    #stop_procs.list(可追加修改)

    startprocessor

    ProcProcessor

    Jackrocessor

    p_mj_deal_cardevent

    13.收集系统信息

    echo --------------------------------------`hostname`-------------------------------------------

    prtconf

    echo -----------lsvg;lsvg `lsvg -o`

    echo "-----------lsvg -l ";lsvg -l `lsvg -o`

    echo "-----------lslv lv ";lsvg -l `lsvg -o`|grep -v "LV NAME"|grep -v awk '{print "lslv "$1}'|sh

    echo -----------df;df -k;lsfs

    echo -------------netstat;netstat -in;netstat -i;netstat -r

    echo -------------------ps;ps -efk;ps gu

    echo ------------------lscfg;lscfg -vp

    echo --------------lssrc;lssrc -a

    echo ------------lsslot;lsslot -c pci

    echo -------------lspv;lspv

    echo -------------lslpp;lslpp -l

    echo -------------lsattr; lsdev -C|awk '{print "echo ---"$1";lsattr -El " $1}'|sh

    echo -------------prtconf -v;prtconf -v

    echo -------------errpt; errpt ;errpt -a

    echo -------------major; ls -al /dev/*

    echo -------------hosts file; cat /etc/hosts

    if [ `ps -ef|grep cluster|grep -v grep|wc -l` -ge 1 ] ;then

    echo -------------HA INFO

    /usr/es/sbin/cluster/utilities/cltopinfo -c;/usr/es/sbin/cluster/utilities/cltopinfo -n;/usr/es/sbin/cluster/utilities/clshowres -n

    `hostname`;/usr/es/sbin/cluster/utilities/cldisp

    fi

    14.将多级子目录的权限放开

    #chmod_dir.sh

    echo for example::chmod_dir.sh rwx /home/mxin/mon

    echo $1$2 >/tmp/chmod_dir.txt

    cat /tmp/chmod_dir.txt|awk  -F / '{print "chmod  o+x /"$2";chmod o+x /"$2"/"$3";chmod o+x /"$2"/"$3"/"$4";chmod o+x /"$2"/"$3"/"$4"/"$5";chmod -R o+"$1" /"$2"/"$3"/"$4"/"$5"/"$6}'|sh

    15.监控oracle是否有锁。

    #mon_db_lock.sh

    . .profile

    cd  /home/mxin/mon

    if [ `date +%H%M` = "0800" ]; then

    echo 0 > warn_count

    fi

    warn_count=`cat warn_count`

    sqlplus "/as sysdba" <<EOF

    set feed off;

    set heading off;

    spool /tmp/db_lock.out1;

    @mon_db_lock.sql

    spool off;

    exit

    EOF

    sleep 13

    sqlplus "/as sysdba" <<EOF

    set feed off;

    set heading off;

    spool /tmp/db_lock.out2;

    @mon_db_lock.sql

    spool off;

    exit

    EOF

    sleep 27

    sqlplus "/as sysdba" <<EOF

    set feed off;

    set heading off;

    spool /tmp/db_lock.out3;

    @mon_db_lock.sql

    spool off;

    exit

    EOF

    cat  /tmp/db_lock.out1|grep -v SQL|grep [0-9] >/tmp/mon_db_lock.out1

    cat  /tmp/db_lock.out2|grep -v SQL|grep [0-9] >/tmp/mon_db_lock.out2

    cat  /tmp/db_lock.out3|grep -v SQL|grep [0-9] >/tmp/mon_db_lock.out3

    cat /tmp/mon_db_lock.out1|grep -v SQL|grep [0-9]|awk '{print "grep ""$0"" /tmp/mon_db_lock.out2"}'|sh >/tmp/db_lock1

    if [ `cat /tmp/db_lock1|wc -l` -gt 0 ] ; then

      cat /tmp/mon_db_lock.out2|grep -v SQL|grep [0-9]|awk '{print "grep ""$0"" /tmp/mon_db_lock.out3"}'|sh >/tmp/db_lock2

      if [ `cat /tmp/db_lock2|wc -l` -gt 0 ] ; then

        cat /tmp/mon_db_lock.out3|grep -v SQL|grep [0-9]|awk '{print "grep ""$0"" /tmp/mon_db_lock.out.old"}'|sh >/tmp/db_lock3

        if [ `cat /tmp/db_lock3|wc -l` -gt 0 ] ; then

        cat /tmp/db_lock3|awk '{print "wall  db lock-------" "$0 ""!!" }'|sh

        let warn_count=$warn_count+1

        fi

      fi

    fi

    cp /tmp/mon_db_lock.out3   /tmp/mon_db_lock.out.old

    if [ $warn_count -gt 4 ] ; then

      beep.sh

      echo 0 > warn_count

    fi

    #mon_db_lock.sql

    set linesize 256

    col object_name  format a18

    col object_id    format 99999999

    col Locked_Mode  format a15

    col SERIAL#      format 9999999

    col session_id   format 999999

    col oracle_username format a15

    col os_user_name format a15

    col process format 9999999

    SELECT substr(b.object_name,1,1 object_name,a.object_id,

    decode( a.locked_mode,

    0, 'None', /* Mon Lock equivalent */

    1, 'Null', /* N */

    2, 'Row-S (SS)', /* L */

    3, 'Row-X (SX)', /* R */

    4, 'Share', /* S */

    5, 'S/Row-X (SSX)', /* C */

    6, 'Exclusive',

    a.locked_mode) Locked_Mode, /* X */

    session_id, SERIAL#,oracle_username, os_user_name, a.process

    FROM v$LOCKED_OBJECT a, dba_OBJECTS b,v$session c

    WHERE a.object_id = b.object_id and a.session_id=c.sid

    /

    16.监控oracle的表空间

    # mon_ts_space.sh

    cd  /home/oraprod

    sqlplus "/as sysdba" <<EOF

    set feed off;

    set heading off;

    spool /tmp/mon_ts_space.out;

    @mon_ts_space.sql

    exit

    EOF

    cat  /tmp/mon_ts_space.out|grep -v SQL|grep [0-9]|awk -f mon_ts_space.awk

    # mon_ts_space.sql

    select

       df.tablespace_name                          "Tablespace",

       (df.totalspace - fs.freespace)              "Used MB",

       fs.freespace                                "Free MB",

       df.totalspace                               "Total MB",

       round(100 * (fs.freespace / df.totalspace)) "Pct. Free"

    from

       dba_tablespaces                               ts,

       (select tablespace_name,

            round(sum(bytes) / 1048576) TotalSpace

          from dba_data_files

          group by tablespace_name)                  df,

       (select tablespace_name,

            round(sum(bytes) / 1048576) FreeSpace

          from dba_free_space

          group by tablespace_name)                 fs

    where

       ts.tablespace_name = fs.tablespace_name

    and

       df.tablespace_name = fs.tablespace_name(+)

    and

    round(100 * (fs.freespace / df.totalspace)) <20

    and df.tablespace_name not in ('APPS_UNDOTS1','TEMP');

    17.只清除所有当前使用该vg的用户

    #kill_vg_user.sh

    if [ $# -le 0  ]  ;then

    echo "no para, example:kill_vg_user.sh erpapp_vg "

    exit

    fi

    df -k|awk '{print $7 }'|grep -v Mounted >/tmp/fs_mounted.txt

    for i in `lsvg -l $1 |grep -vE "N/A|vg|MOUNT"|awk '{print $7}'`

    do

    if [ `grep -c $i /tmp/fs_mounted.txt`  -ge 1 ] ; then

      echo fuser -kc $i

       fuser -kc $i

    fi

    done

    18.每月月底执行的脚本

    #month_lastday.sh

    . .profile

    TZ=TZ-24

    echo `date +%d`

    if [ `date +%d` = "01" ]; then

    echo "ok. today is last day of this month. run it!"

    #insert your shell scripts

    fi

    19.每月1日执行的脚本

    #month_firstdy.sh

    . .profile

    TZ=TZ+24

    echo `date +%d`

    if [ `date +%d` = "02" ]; then

    echo "ok. today is firstday of  this month. run it!"

    #insert your shell scripts

    fi

    20.跟踪oracle export结果的脚本(放在exp脚本最后)

    #exp_check.sh

    if [ `tail /tmp/exp_dvlp.log|grep " success"|wc -l` -lt 1 ];then

      echo   "db dvlp export fail!!!"  #报警

      wall    "db dvlp export fail!!!"

    fi

    21.检查系统的进程

    a.check_proc.sh:

    #check_proc.sh

    cat check_proc.list|awk -F "," {'print   "Check_proc.sh  "$1" ""$2""" "   "$3 '}|sh

    b.check_proc.list:(可修改)

    telnet,telnetd -a,2

    xcom,xcommanager.py,1

    c.Check_proc.sh:

    #Check_proc.sh

    export LANG=en_US

    count=`ps -ef|grep "$2"|grep -v grep|wc -l`

    if [ $count -lt $3 ];then

        echo $1 has  not be started all,the number is $count/$3!

    fi

    运行示例:

    [test3][root][/home/mxin]>ps -ef           

         UID    PID   PPID   C    STIME    TTY  TIME CMD

        root      1      0   0   Aug 10      -  0:11 /etc/init

        root  77910      1   0   Aug 10      -  5:53 /usr/sbin/syncd 60

        root 102470      1   0   Aug 10      -  0:00 /usr/ccs/bin/shlap64

        root 106572      1   0   Aug 10      -  0:00 /usr/lib/errdemon

        root 139366      1   0   Aug 10      -  0:00 /usr/sbin/srcmstr

        root 164068 139366   0   Aug 10      -  0:00 /usr/sbin/snmpd

        root 184466 139366   0   Aug 10      -  4:38 /usr/sbin/aixmibd

        root 246002      1   0   Aug 10   vty0  8:50 -ksh

        root 409612 139366   0   Aug 10      -  0:05 sendmail: accepting connections

        root 417830 139366   0   Aug 10      -  0:00 /usr/sbin/syslogd

        root 421898      1   0   Aug 10      -  0:00 /usr/sbin/uprintfd

        root 442602 782494   0 16:19:29      -  0:00 telnetd -a

        root 446688 442602   0 16:19:29  pts/1  0:00 -ksh

        root 466976 782494   0   Sep 07      -  0:00 ftpd

        root 471108 139366   0   Aug 10      -  0:01 /usr/sbin/hostmibd

        root 495680      1   0   Aug 10      -  0:04 /usr/sbin/cron

        root 508018 139366   0   Aug 10      -  0:00 /usr/sbin/portmap

        root 708686 782494   0   Sep 07      -  0:00 ftpd

        root 729196 139366   0   Aug 10      -  0:04 /usr/sbin/snmpmibd

        root 733290 139366   0   Aug 10      -  0:00 /usr/sbin/muxatmd

        root 762026 446688   0 17:46:17  pts/1  0:00 ps -ef

        root 782494 139366   0   Sep 07      -  0:00 /usr/sbin/inetd

    [test3][root][/home/mxin]>check_proc.sh    

    telnet has not be started all,the number is 1/2!

    xcom has not be started all,the number is 0/1!

    22.文件中抽取指定字符,并定向到新文件

    举例:从监控结果中抽取PGA使用情况的数据,这个数据的特点是所在行只有一个变量

    #!/usr/bin/ksh

    #Script name: Grep specific word from a file

    #Usage: filegrep filename

    file_time=$(date +%Y%m%d%H%M)

    newfile=${PWD}/$1.$file_time

    lines=$(cat $1 |wc -l)

    #######################################################################

    cat $1 | while read LINE

    do

    if [[ -n "$LINE" ]] && (print $LINE|grep -v ^# >/dev/null)

        then

            set $LINE

            if (( $# == 1 ))

                then

                    print $LINE |grep -v "SUM(S.PGA_USED_MEM)" | awk '{print $1}' >>$newfile

            fi

    fi

    done

    exit 0

    扩展用法:除了PGA使用情况的前面添加时间。

    时间所在行有两个变量,可以将时间和PGA使用数值同时过滤出来,定义到$$.tmp1,然后将时间和PGA使用数值打印到同一行,并用tab键隔离开,定向到$$.tmp2,这时,对于每一行,$1与$2都是时间,而$3是PGA使用数值,故对每一行只打印$1与$3,定向到$newfile。

    #!/usr/bin/ksh

    #Script name: Grep specific word from a file

    #Usage: filegrep filename

    file_time=$(date +%Y%m%d%H%M)

    cat $1|grep -v weblogic|grep -v ^SQL|grep -v -|grep -v SUM |grep -v SID|grep -v rows |grep -v Copy|grep -v With|grep -v and>>$$.tmp

    newfile=${PWD}/$1.$file_time

    lines=$(cat $1 |wc -l)

    #######################################################################

    cat $$.tmp | while read LINE

    do

            if [[ -n "$LINE" ]] #&& (print $LINE|grep -v ^# >/dev/null)

            then

                    set $LINE

                    if (( $# < 3 ))

                    then

                            print $LINE |grep -v "SUM(S.PGA_USED_MEM)" | awk '{print $1}' >>$$.tmp1

                    fi

            fi

    done

    rm $$.tmp

    #######################################################################

    cat $$.tmp1 | while read LINE

    do

            if [[ -n "$LINE" && -n `print $LINE|grep ":"` ]]

            then

                    print -n "${LINE} " >>$$.tmp2

            else

                    print "${LINE} " >>$$.tmp2

            fi

    done

    rm $$.tmp1

    #######################################################################

    cat $$.tmp2 | while read LINE

    do

            set $LINE

            print "$1 $3" >>$newfile

    done

    rm $$.tmp2

    exit 0

    按行号循环整理数据,实例:每8行输出到一行,循环执行:

    #!/usr/bin/ksh

    #Script name: Grep specific word from a file

    #Usage: filegrep filename

    file_time=$(date +%Y%m%d%H%M)

    newfile=${PWD}/$1.$file_time

    cat $1 |grep -v ^*|grep -v ALERT|grep -v "GBL_SWAP_SPACE_AVAIL" |awk '{print $2" "$3" "$5" "}' >>$$.tmp

    #######################################################################

    i=1

    cat $$.tmp | while read LINE

    do

        if [[ -n "$LINE" && $i -lt 9 ]]

        then

            if [[ $i != 8 ]]

            then

                print -n "${LINE} " >> $newfile

            else

                print "${LINE}" >> $newfile

            fi

        ((i+=1))

        else

            print -n "${LINE} " >> $newfile

            i=2

        fi

    done

    rm $$.tmp

    exit 0

  • 相关阅读:
    linux安装kafka教程
    linux 系统java相关部署
    redies学习总结
    Sentinel自定义异常降级-新旧版本差异
    Android Bitmap压缩详解
    Head First之策略模式
    go测试
    go获取命令行参数
    JVM-垃圾收集算法基础
    Java代理模式
  • 原文地址:https://www.cnblogs.com/mycftest/p/15520999.html
Copyright © 2011-2022 走看看