Shell腳本抽取實例_第1頁
Shell腳本抽取實例_第2頁
Shell腳本抽取實例_第3頁
Shell腳本抽取實例_第4頁
Shell腳本抽取實例_第5頁
已閱讀5頁,還剩25頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領

文檔簡介

1、Shell腳本抽取實例 #!/usr/bin/ksh1. 應用地址變量引用myself="backup_chncm_file.sh"logfile="$MCB_HOME/$MCB_APPID/var/log/$procname.log"2. case實例case $MCB_APPID inchs)ic_dir="/opt/mcb/chs/data/*/inroam/incoming"arch_dir="/opt/mcb/chs/back"file_pa

2、ttern="ACCD?CHNCM*"db_user="chsdba"ops)ic_dir="/opt/mcb/ops/data/incoming/ics"arch_dir="/opt/mcb/ops/arch/incoming/ics"file_pattern="ACCD?CHNCM*"db_user="iogsmdba"*) MCB_APPID=dps1echo &qu

3、ot;MCB_APPID is not in chs/ops, please check!"exit 1;esac3. 判斷參數(shù)個數(shù)實例if $# -lt 1 ;thenecho "SERIOUS:DBMSG() need 1 argument: DEBUGMSG"return 1fi4. 判斷結(jié)果實例if $? -ne 0 ;thenmessage="query carriers table failed! db_user=$db_user db_passwd=$db_passwd db_sid=int1&quo

4、t;write_log "SERIOUS"exit 1fi5. 判斷目錄實例if ! -d $carrier_arch_dir ;thenmkdir -p $carrier_arch_dirfiif -d $DirPath -a -r $DirPath -a -w $DirPath thenreturn 0elseecho "Directory:$DirPath not exists or is not writeable"exit 1fi5. 判斷文件實例if -z "$bassFile&quo

5、t; ;thenecho "SERIOUS:unitcode in config file is null! exit"exit 1fi6. getdbpwd實例db_passwd="$(getdbpwd $db_user)"if $? -ne 0 -o -z "$db_passwd" ;thenmessage="getdbpwd $db_user failed!"write_log "SERIOUS"return

6、1fi7. and實例if not_open_flag -eq 1 -a have_arch_before -eq 1 ;thenfi8. or實例if not_open_flag -eq 1 -o have_arch_before -eq 1 ;thenfi9. find實例#查找文件匹配find . -type f -name "$file_pattern"#查找目錄匹配并且消除notice/match目錄查找find . -type d "/notice/match/" -prune#查找目錄下面比$tagfile晚

7、的文件find $ic_dir -type f -name "$file_pattern" ! -newer $tagfilefind $dir -mtime +$days -type f -exec rm -f ;find $dir -mtime -$days -type f -exec ls -l ;10. basename文件名實例FullPath=/opt/mcb/ibip/sourcefile.gzFileName=basename $FullPath10. 配置文件實例configfile=$MCB_HOME/$MCB_APPID/conf/$m

8、odulename.conf. ./cfConfigFile.shcfInit $configfile noLock readOnlyftpip=$(cfGetConfigItem common ftphost)ftpuser=$(cfGetConfigItem common ftpuser)ftpdir=$(cfGetConfigItem common remotedir)10. sort排序?qū)嵗齦s -r $src_dir | sort +7find $report_dir -type f -name "f*_$lastDate_$unit_code_*&quot

9、; | sort -r | read lastSentFile10. 剪切實例mv -f $pathfile $arch_dir/$file_carrier_cd/10. tr實例#大小寫轉(zhuǎn)換實例carrier_cd="$(echo $1|tr :lower: :upper:)"#刪除空行cat $file | tr -s "n" > $matchfile#M替換為換行cat $file | tr -s "015" "n" &gt

10、; $matchfile 10. sed替換實例firstPara=$(echo $firstPara | sed 's/base/model/g')#打印第一行sed -n '1p' match.txt#刪除第一行sed '1d' match.txt#打印最后一行sed -n '$p' ma tch.txt#刪除最后一行sed '$d' match.txt10. Shell數(shù)據(jù)庫實例sqlplus -S /nolog <

11、<EOF>$sqllogfileconnect $db_user/$db_passwdchsset head offinsert into $dup_all_dbuser.$dup_all_tab_nm$dup_all_dblink select * from $inst_name_tmp_table ;commit;select cust_carrier_cd from partner_carriers where partner_carrier_cd='CHNCM' and biz_pkg=1 and effc_tm&lt

12、;=SYSDATE and expired_tm>=SYSDATE and open_dt<=SYSDATE and close_dt>=SYSDATE;EOFif $? -ne 0 ;thenmessage="query carriers table failed! db_user=$db_user db_passwd=$db_passwd db_sid=chs"write_log "SERIOUS"exit 1fiecho "commit;"

13、 >>$tmpsqlpath/insert.sqlecho "spool $reportlst" >>$tmpsqlpath/insert.sqlecho "/" >>$tmpsqlpath/insert.sqlecho "spool off" >>$tmpsqlpath/insert.sqlsqlplus -S /nolog <<EOF 1&

14、;gt;/dev/null 2>&1connect $sv_userid/$sv_passwordint1$tmpsqlpath/insertEOF10. touch實例touch -t $(getAnyTime -h -1 -o YYYYMMDDHHMI) $tagfile11. ftp實例sendfile()>/tmp/.$MySelf.logksh >>/tmp/.$MySelf.log << SHEOFftp -ivn $ftp_ip <<EOFuser $f

15、tp_user $ftp_passbinlcd $_local_dircd $_remote_dirpromptput $_filename .$_filenamerename .$_filename $_filenamebyeEOFSHEOFgrep "Transfer complete" /tmp/.$MySelf.log > /dev/null 2>&1if $? -eq 0 thenreturn 0elseecho "SERIOUS:ftp failed to $ftp_ip&qu

16、ot;return 1fireceivefile()ftp -i -n $ftp_ip <<EOFuser $ftp_user $ftp_passbinlcd $_local_dircd $_remote_dirpromptmget $_filenamebyeEOFreturn 0deletefile()ftp -i -n $ftp_ip <<EOFuser $ftp_user $ftp_passbincd $_remote_dirpromptdelete $_filenamebyeEOFreturn 0ksh>&g

17、t;$TransLog<<SHEOFftp -ivn $_ftp_ip <<EOFuser $_ftp_user $_ftp_passbincd $_remote_dirmls $_file_pattern $_local_filebyeEOFSHEOF12. cut截取實例echo $sentFileName | cut -d. -f 1 | read sentFilePrifixf_reportFile="f_$sentFilePrifix.verf"echo $sentFileName | cut

18、 -d_ -f 1-5 | read valFileFixr_reportFile="r_$valFileFix.verf"echo $f_prifix | cut -d_ -f 2- | read f_dat13. awk分隔符實例echo $line | awk 'print substr($0,55,2)' | read valCodeecho $line | awk -F: 'print $NF' | wc -c | read fieldCntfile_carrier_cd=$(echo

19、 "$pathfile"|awk -F/ 'print $NF'|cut -c3-7)echo $line | awk -F: 'print $NR' | read rowNum13. wc計數(shù)實例echo $line | awk -F: 'print $NF' | wc -l | read valCode14. 時間日期實例fileDate=date +%Y%m%decho $fileDate | cut -c 1-4 | read yearecho $file

20、Date | cut -c 5-6 | read monthecho $fileDate | cut -c 7-10 | read mdateslastDay=preday -1 date $fileDatedddate=date +"%Y%m%d-%H:%M:%S"senddate=$bin_path/preday 1 datesendmonth=$bin_path/preday 10 month13. while實例cat $f_valReport| while read linedoif "$valCode" = &

21、amp;quot;00" ;thenlgWriteLog INFO "" 0 "file=$sentFileName has passed file level validat ion according to validation report:$f_valReport"elselgWriteLog SERIOUS "" 1 "file=$sentFileName has File Level Errors,error code is $valCod

22、e,the validation report is $f_valReport"return 1fidone14. substr截取字符串實例echo $line | awk 'print substr($0,55,2)' | read valCode16. gzip壓縮實例/usr/contrib/bin/gzip -c $src_dir$filename > $src_dir/.$filenameif $? -ne 0 ;thenlgWriteLog SERIOUS "" 0 &qu

23、ot;gziping file=$filename failed !"return 1fimv $src_dir/.$filename $arch_dir/$filename17. 設置環(huán)境變量export PATH=$PATH:/usr/contrib/bin/19. 設置鎖Lock實例PIDLock=$MCB_HOME/$MCB_APPID/var/lock/.$MySelf.lckif -r $PIDLock ; thencat $PIDLock | read pidkill -0 $pid > /dev/null 2>&1i

24、f $? -eq 0 ; thenecho "The same program $MySelf.sh is running now."lgWriteLog SERIOUS "" 1 "The same program $MySelf.sh is running now,exit!"exit 1fifiecho $ > $PIDLock20. 檢查網(wǎng)絡狀態(tài)ping實例pingnum=/etc/ping $ftp_ip -n 10|wc -lif ! "$pi

25、ngnum" = "4" ;then( LostPerc=(15-$pingnum)*10 )elselgWriteLog SERIOUS "" 1 "$(hostname) can not reach $ftp_ip,100% packet loss,exit!"exit 1fiif $LostPerc -gt 40 ;thenlgWriteLog SERIOUS "" 1 "$(hostname) to $ftp

26、_ip $LostPerc% packet loss,bad status of network,exit!"exit 1fi21. for循環(huán)實例for unit in $unitcodedoreceivefile $temp_dir *_$unit_* $remote_reportif $? -ne 0 ;thenlgWriteLog SERIOUS "" 1 "Failed to fetch validate report files from bass,exit"exit 1fidone22. 字

27、符串數(shù)組實例unset failUnitCode*unset sendUnitFileTimes* count=0sendTimes=0ls -r $src_dir | sort | while read ffdo#check whether current unit code need to sendlen=$#failUnitCode*i=0while $i -lt $len doif "$failUnitCode$i" = "$unit_code" ;thensendFlag="false&

28、quot;breakfi(i=$i+1)done23. grep查找字符串實例grep "abc*" $file_namezgrep "abc*" $file_name.gz24. exp導出分區(qū)if "$DB_SID" = "-" ;thenconnStr="$DB_USER/$DB_PWD"elseconnStr="$DB_USER/$DB_PWD$DB_SID"fiexp $

29、connStr tables=$TAB_NM:$partName file=$DBArchDir/$partName_$DB_USER_$TAB_NM.dmp grants=n indexes=n buffer=10240000 log=$ExpLogDir/$partName_$DB_USER_$TAB_NM-exp.logif $? -ne 0 thenMsg="export $DB_USER/$DB_PWD tables=$TAB_NM:$partName failed!"LogMsg 4 "$Msg"( retur

30、nCode += 1 )return 1figzip -f $DBArchDir/$partName_$DB_USER_$TAB_NM.dmp25. 引用讀取配置ExpConf=$MCB_HOME/$MCB_APPID/conf/cdr_part_exp.confif ! -f $ExpConf ;thenecho "config file not exist!"exit 1fi. cfConfigFile.sh. ilogger.shcfInit $ExpConf noLock readOnlyif $? -ne 0 ;thenecho &quot

31、;Init config error!"exit 1fi#LogDir=$(cfGetConfigItem common LogDir) ArchDir=$(cfGetConfigItem common ArchDir)ExpLogDir=$(cfGetConfigItem common DBLogDir)cdr_part_exp.conf%commonLogDir=/opt/mcb/ips/var/log/ArchDir=/opt/mcb/ips/arch/DB/DBLogDir=/opt/mcb/ips/var/log/dblogs26. ps檢查進程實例CheckIns

32、t()binName=$1instName=$2ps -e|grep " $binName$"|while read pid xx1 xx2dops -f -p $pid|grep "$instName"|grep -v "grep"|wc -l|read icntif "$icnt" -ne "0" ;thenlgWriteLog 1 CtrlCenter 00000000 "異步入庫應用服務的實

33、例$InstName進程正在運行!"echo "異步入庫應用服務的實例$InstName進程正在運行!"return 0fidonereturn 127. shell編寫存儲過程TmpFile=$TmpFilePrefix$CleanDur.lstsqlplus -S /nolog 2>/dev/null << !connect $dbuser/$dbpwd$dbsidspool $TmpFilePath/$TmpFileDECLARE/* clean_type D:daily W:weekly M

34、:monthly Y:year */clean_type VARCHAR2(3) := '$TableType'clean_time DATE;BEGINLOOPIF clean_type = 'D' THENclean_time := trunc(sysdate-($CleanCycle),'dd');DELETE FROM $CleanTable WHERE $CleanTableColumn < clean_time AND rownum<5000;END IF;I

35、F sql%notfound THENexit;END IF;COMMIT;END LOOP;END;/quit!28. 數(shù)據(jù)庫連接SQL語句sqlplus -s "$dbuser/$dbpasswd$dbsid" <<-! >$logtmp1create table $inst_name_tmp_table as select * from $dbuser.$dup_tab_nm partition($part_name);insert into $dup_all_dbuser.$dup_all_tab_nm$du

36、p_all_dblink select * from $inst_name_tmp_table ;commit;!sqlplus -s "$dbuser"/"$dbpasswd""$dbsid" <<-EOF >$lv_tmpfile 2>&1insert into $dbuser.fileval_err_info ( ic_file_nm, file_type, prov_cd, pcs_recv_tm, err_

37、cd, err_type,src_line, proc_nm ) values ('$1', '$2', '$3', to_date($4,'yyyy-mm-dd hh24:mi:ss'), '$5', '$6', '$7', '$8');commit;exitEOF29. rcp遠程操作errmsg=rcp $outfile $sz_ics

38、_host:$sz_ics_dir/IntFtamFtp.confif $? != 0 ;thenlgWriteLog "ALARM" "" -1 "rcp errmsg $errmsg"elselgWriteLog "SUCC" "" 0 "rcp %s to %s successfully." "$outfile" "$sz

39、_ics_host:$sz_ics_dir/IntFtamFtp.conf"fi30. getftppwd獲取密碼ftp_pass=$(getftppwd $ftp_ip $ftp_user)31. 導出dmp文件實例DBMSG "exp $dbuser/$dbpwd$dbsid file=$localdir/$dmpfile buffer=10240000 log=$log_file tables=$tablelist."exp $dbuser/$dbpwd$dbsid file=$localdir/$dmpfile buffer=102

40、40000 log=$log_file tables=$tablelist grants=n indexes=n triggers=n33. 數(shù)據(jù)庫引用sql操作echo "commit;" >>$tmpsqlpath/insert.sqlecho "spool $reportlst" >>$tmpsqlpath/insert.sqlecho "/" >>$tmpsqlpath/insert.sqlech

41、o "spool off" >>$tmpsqlpath/insert.sqlsqlplus -S /nolog <<EOF 1>/dev/null 2>&1connect $sv_userid/$sv_passwordint1$tmpsqlpath/insertEOF*practise shell* *授權(quán)命令:chmod 4+2+1 /opt/mcb/ips/myfile4代表可讀,2代表可寫,1代表可執(zhí)行授權(quán)目錄:chmod -R 4+2+1 /opt/m

42、cb/wbs轉(zhuǎn)移用戶文件歸屬:chown filename owner轉(zhuǎn)移用戶目錄歸屬:chown -R dir owner轉(zhuǎn)移用戶組文件歸屬:chgrp filename groupA轉(zhuǎn)移用戶組目錄歸屬:chgrp -R dir groupBfind命令:find . -name "*.txt" -查找當前目錄下面的文本文件find /opt/mcb/wbs -name "*.txt" -查找指定目錄下面的文本文件find /opt/mcb/wbs -type f -name "*.txt&qu

43、ot;-type 查找某一類型的文件,諸如:b - 塊設備文件。d - 目錄。c - 字符設備文件。p - 管道文件。l - 符號鏈接文件。f - 普通文件。find /opt/mcb/wbs -type f -name "*.log" -exec rm ; -找出后執(zhí)行命令查看進程命令:ps -ef | grep "java"殺進程命令:kill 28679用戶推出不掛起操作:nohup command控制臺輸入讀?。篹cho -n "Can I clear your saved files(y/n):

44、"read answerecho "your choise is "$answer重定向輸出:echo "Hello World In Java" >> abc.txt轉(zhuǎn)義字符:echo ""/dev/rmt0""cat命令:cat passwd |awk -F: 'print $1" and "$2" over all shutdown

45、."'正則表達式:A-Z -以大寫字母打頭A-Za-z -以字母打頭0-9 -以數(shù)字打頭A-Za-z0-9 -以數(shù)字或者字母打頭trouble$ -以trouble結(jié)尾A2B -匹配字母A出現(xiàn)兩次A4,B -匹配字母A最少出現(xiàn)四次A2,4B -匹配字母A出現(xiàn)2到4次0-9A-Za-z -非數(shù)字和字母打頭Ccomputer -匹配Computer或者computergrep命令:grep -i abc myfile -匹配abc字符串,不區(qū)分大小寫grep -n abc myfile -輸出匹配abc字符串信息,并打印出序號grep -n '$&

46、amp;#39; myfile -輸出空行序號awk命令:cat myfile | awk -F: 'print "rowline"NR" "$1" and "$2' | read abstr -NR代表序號echo $abstrawk內(nèi)置字符串函數(shù):gsub(r,s) 在整個$ 0中用s替代rawk 'gsub(4849,1225) print $0' grade.txtindex(s,t) 返回s中字符串t的第一位置aw

47、k 'print index("teckwon831227","8")' grade.txtlength(s) 返回s長度awk -F: 'print length($3)' grade.txtmatch(s,r) 測試s是否包含匹配r的字符串a(chǎn)wk 'print match($3,"abc")' grade.txtsplit(str,arr,fs) 以fs作為分割符將str字符串分成數(shù)組ar

48、r,并返回下標awk 'print split(AD2-KP9-JU2-LP1,myarr,"-")'echo $? -輸 出4myarr1=AD2myarr2=KP9myarr3=JU2myarr4=LPsubstr(s,p) 返回字符串s中從p開始的后綴部分substr(s,p,n) 返回字符串s中從p開始長度為n的后綴部分awk 'print substr("abc123teckwon","123",4)' -輸出te

49、cksort命令:video.txt:teckwon:HK:23:12580fanyuan:CH:21:89654Alian:FR:30:87555pangpang:EN:20:80000field0 field1 field2 field3 teckwon HK 23 12580fanyuan CH 21 89654sort -t: video.txt -按照:作為分隔符,默認按照field0進行升序排序sort -t: -r video.txt -按照降序進行排序sort -t: +1 video.txt -按照field1進行升序排序sort -t: +2n video.txt -數(shù)值排

50、序要加上n,然后進行field2數(shù)值升序排序sort -t: -u video.txt -去重并按照field0進行升序排序sort -t: -r -k2 k1 video.txt -按照k2和k1域進行降序排序df | sort -b -r +4 -按照field4進行降序排序sort -t: video.txt | head -2 -排序后只是列出前2行sort -t: video.txt | tail -4 -排序后只列出末尾4行uniq命令:uniq -u myfile.txt -只顯示不重復行uniq -d myfile.txt -只顯示重復行uniq -c myfile.txt -只顯示每一重

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論