[shell编程] AIX shell工具集

AIX

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

实验1.txt 2.txt

ls -l *.txt |awk {m=$9;gsub(/txt/,"com",$9);print"cp " m " "$9}

cp " m " "$9中间有空格

cp 1.txt 1.com

cp 2.txt 2.com


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 }

  1. chmod 755 showuser
  2. cp showuser /usr/bin
  3. 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.相当于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


1.批量改名或拷贝文件

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

拷贝

先查看

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

for i in ​​echo start*.sh​

do

cp $i ​​echo $i|sed s/start/stop/g​​ #拷贝

mv $i ​​echo $i|sed s/start/stop/g​​ #改名

done


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

rmlog.sh

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

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

find /recycle -mtime +7 -exec rm ...

find /var/tmp/ /recyle /dir_whatever -mtime +7 -exec rm -rf {} ;


  1. 察看根目录各文件和子目录大小,去除文件系统统计
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


引用:原帖由 lingam 于 2006-6-21 01:12 发表

find /var/tmp/ /recyle /dir_whatever -mtime +7 -exec rm -rf {} ;

这个恐怕不能完全简化,比如X11文件我希望不能清除等等。


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


  1. 保存清理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"

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,18) 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"

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


19.跟踪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


20.检查系统的进程

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!