[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!