最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 正文

Shell脚本抽取实例

来源:动视网 责编:小OO 时间:2025-09-26 18:13:12
文档

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_pattern="[AC][CD]?CHNCM*"db_use
推荐度:
导读#!/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_pattern="[AC][CD]?CHNCM*"db_use


#!/usr/bin/ksh

1. 应用地址变量引用

myself="backup_chncm_file.sh"

logfile="${MCB_HOME}/${MCB_APPID}/var/log/${procname}.log"

2. case实例

case ${MCB_APPID} in

chs)

ic_dir="/opt/mcb/chs/data/*/inroam/incoming"

arch_dir="/opt/mcb/chs/back"

file_pattern="[AC][CD]?CHNCM*"

db_user="chsdba"

;;

ops)

ic_dir="/opt/mcb/ops/data/incoming/ics"

arch_dir="/opt/mcb/ops/arch/incoming/ics"

file_pattern="[AC][CD]?CHNCM*"

db_user="iogsmdba"

;;

*) MCB_APPID=dps1

echo "MCB_APPID is not in chs/ops, please check!"

exit 1

;;

esac

3. 判断参数个数实例

if [ $# -lt 1 ];then

echo "SERIOUS:DBMSG() need 1 argument: DEBUGMSG"

return 1

fi

4. 判断结果实例

if [ $? -ne 0 ];then

message="query carriers table failed! db_user=${db_user} db_passwd=${db_passwd} db_sid=int1"

write_log "SERIOUS"

exit 1

fi

5. 判断目录实例

if [ ! -d ${carrier_arch_dir} ];then

mkdir -p ${carrier_arch_dir}

fi

if [ -d $DirPath -a -r $DirPath -a -w $DirPath ]

then

return 0

else

echo "Directory:$DirPath not exists or is not writeable"

exit 1

fi

5. 判断文件实例

if [ -z "$bassFile" ];then

echo "SERIOUS:unitcode in config file is null! exit"

exit 1

fi

6. getdbpwd实例

db_passwd="$(getdbpwd ${db_user})"

if [ $? -ne 0 -o -z "${db_passwd}" ];then

message="getdbpwd ${db_user} failed!"

write_log "SERIOUS"

return 1

fi

7. and实例

if [ not_open_flag -eq 1 -a have_arch_before -eq 1 ];then

fi

8. or实例

if [ not_open_flag -eq 1 -o have_arch_before -eq 1 ];then

fi

9. find实例

##查找文件匹配

find . -type f -name "$file_pattern"

##查找目录匹配并且消除notice/match目录查找

find . -type d "/notice/match/" -prune

##查找目录下面比$tagfile晚的文件

find ${ic_dir} -type f -name "$file_pattern" ! -newer $tagfile

find $dir -mtime +$days -type f -exec rm -f {} \\;

find $dir -mtime -$days -type f -exec ls -l {} \\;

10. basename文件名实例

FullPath=/opt/mcb/ibip/sourcefile.gz

FileName=`basename $FullPath`

10. 配置文件实例

configfile=$MCB_HOME/${MCB_APPID}/conf/${modulename}.conf

. ./cfConfigFile.sh

cfInit ${configfile} noLock readOnly

ftpip=$(cfGetConfigItem common ftphost)

ftpuser=$(cfGetConfigItem common ftpuser)

ftpdir=$(cfGetConfigItem common remotedir)

10. sort排序实例

ls -r $src_dir | sort +7

find $report_dir -type f -name "f*_${lastDate}_${unit_code}_*" | sort -r | read lastSentFile

10. 剪切实例

mv -f ${pathfile} ${arch_dir}/${file_carrier_cd}/

10. tr实例

##大小写转换实例

carrier_cd="$(echo $1|tr [:lower:] [:upper:])"

##删除空行

cat $file | tr -s ["\

"] > $matchfile

##^M替换为换行

cat $file | tr -s "[\\015]" "\

" > $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.txt

10. Shell数据库实例

sqlplus -S /nolog <${sqllogfile}

connect ${db_user}/${db_passwd}@chs

set head off

insert 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<=SYSDATE and expired_tm>=SYSDATE and open_dt<=SYSDATE and close_dt>=SYSDATE;

EOF

if [ $? -ne 0 ];then

message="query carriers table failed! db_user=${db_user} db_passwd=${db_passwd} db_sid=chs"

write_log "SERIOUS"

exit 1

fi

echo "commit;" >>$tmpsqlpath/insert.sql

echo "spool $reportlst" >>$tmpsqlpath/insert.sql

echo "/" >>$tmpsqlpath/insert.sql

echo "spool off" >>$tmpsqlpath/insert.sql

sqlplus -S /nolog </dev/null 2>&1

connect $sv_userid/$sv_password@int1

@$tmpsqlpath/insert

EOF

10. touch实例

touch -t $(getAnyTime -h -1 -o YYYYMMDDHHMI) ${tagfile}

11. ftp实例

sendfile()

{

>/tmp/.${MySelf}.log

ksh >>/tmp/.${MySelf}.log << SHEOF

ftp -ivn $ftp_ip <user $ftp_user $ftp_pass

bin

lcd ${_local_dir}

cd ${_remote_dir}

prompt

put ${_filename} .${_filename}

rename .${_filename} ${_filename}

bye

EOF

SHEOF

grep "Transfer complete" /tmp/.${MySelf}.log > /dev/null 2>&1

if [ $? -eq 0 ]

then

return 0

else

echo "SERIOUS:ftp failed to $ftp_ip"

return 1

fi

}

receivefile()

{

ftp -i -n $ftp_ip <user $ftp_user $ftp_pass

bin

lcd ${_local_dir}

cd ${_remote_dir}

prompt

mget ${_filename}

bye

EOF

return 0

}

deletefile()

{

ftp -i -n $ftp_ip <user $ftp_user $ftp_pass

bin

cd ${_remote_dir}

prompt

delete ${_filename}

bye

EOF

return 0

}

ksh>>${TransLog}<ftp -ivn ${_ftp_ip} <user ${_ftp_user} ${_ftp_pass}

bin

cd ${_remote_dir}

mls ${_file_pattern} ${_local_file}

bye

EOF

SHEOF

12. cut截取实例

echo $sentFileName | cut -d. -f 1 | read sentFilePrifix

f_reportFile="f_$sentFilePrifix.verf"

echo $sentFileName | cut -d_ -f 1-5 | read valFileFix

r_reportFile="r_$valFileFix.verf"

echo $f_prifix | cut -d_ -f 2- | read f_dat

13. awk分隔符实例

echo $line | awk '{print substr($0,55,2)}' | read valCode

echo $line | awk -F: '{print $NF}' | wc -c | read fieldCnt

file_carrier_cd=$(echo "$pathfile"|awk -F/ '{print $NF}'|cut -c3-7)

echo $line | awk -F: '{print $NR}' | read rowNum

13. wc计数实例

echo $line | awk -F: '{print $NF}' | wc -l | read valCode

14. 时间日期实例

fileDate=`date +%Y%m%d`

echo $fileDate | cut -c 1-4 | read year

echo $fileDate | cut -c 5-6 | read month

echo $fileDate | cut -c 7-10 | read mdates

lastDay=`preday -1 date ${fileDate}`

dddate=`date +"%Y%m%d-%H:%M:%S"`

senddate=`${bin_path}/preday 1 date`

sendmonth=`${bin_path}/preday 10 month`

13. while实例

cat ${f_valReport}| while read line

do

if [ "$valCode" = "00" ];then

lgWriteLog INFO "" 0 "file=[$sentFileName] has passed file level validat

ion according to validation report:$f_valReport"

else

lgWriteLog SERIOUS "" 1 "file=[$sentFileName] has File Level Errors,error code is [${valCode}],the validation report is $f_valReport"

return 1

fi

done

14. substr截取字符串实例

echo $line | awk '{print substr($0,55,2)}' | read valCode

16. gzip压缩实例

/usr/contrib/bin/gzip -c ${src_dir}$filename > ${src_dir}/.$filename

if [ $? -ne 0 ];then

lgWriteLog SERIOUS "" 0 "gziping file=[${filename}] failed !"

return 1

fi

mv ${src_dir}/.$filename ${arch_dir}/$filename

17. 设置环境变量

export PATH=$PATH:/usr/contrib/bin/

19. 设置锁Lock实例

PIDLock=$MCB_HOME/${MCB_APPID}/var/lock/.${MySelf}.lck

if [ -r $PIDLock ]; then

cat $PIDLock | read pid

kill -0 $pid > /dev/null 2>&1

if [ $? -eq 0 ]; then

echo "The same program ${MySelf}.sh is running now."

lgWriteLog SERIOUS "" 1 "The same program ${MySelf}.sh is running now,exit!"

exit 1

fi

fi

echo $$ > $PIDLock

20. 检查网络状态ping实例

pingnum=`/etc/ping $ftp_ip -n 10|wc -l`

if [ ! "$pingnum" = "4" ];then

(( LostPerc=(15-$pingnum)*10 ))

else

lgWriteLog SERIOUS "" 1 "$(hostname) can not reach $ftp_ip,100% packet loss,exit!"

exit 1

fi

if [ $LostPerc -gt 40 ];then

lgWriteLog SERIOUS "" 1 "$(hostname) to $ftp_ip ${LostPerc}% packet loss,bad status of network,exit!"

exit 1

fi

21. for循环实例

for unit in $unitcode

do

receivefile $temp_dir *_${unit}_* $remote_report

if [ $? -ne 0 ];then

lgWriteLog SERIOUS "" 1 "Failed to fetch validate report files from bass,exit"

exit 1

fi

done

22. 字符串数组实例

unset failUnitCode[*]

unset sendUnitFileTimes[*]

count=0

sendTimes=0

ls -r $src_dir | sort | while read ff

do

#check whether current unit code need to send

len=${#failUnitCode[*]}

i=0

while [ $i -lt $len ]

do

if [ "${failUnitCode[$i]}" = "${unit_code}" ];then

sendFlag="false"

break

fi

((i=$i+1))

done

23. grep查找字符串实例

grep "abc*" $file_name

zgrep "abc*" $file_name.gz

24. exp导出分区

if [ "$DB_SID" = "-" ];then

connStr="$DB_USER/$DB_PWD"

else

connStr="$DB_USER/$DB_PWD@$DB_SID"

fi

exp $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.log

if [ $? -ne 0 ]

then

Msg="export $DB_USER/$DB_PWD tables=${TAB_NM}:${partName} failed!!"

LogMsg 4 "$Msg"

(( returnCode += 1 ))

return 1

fi

gzip -f ${DBArchDir}/${partName}_${DB_USER}_${TAB_NM}.dmp

25. 引用读取配置

ExpConf=$MCB_HOME/$MCB_APPID/conf/cdr_part_exp.conf

if [ ! -f $ExpConf ];then

echo "config file not exist!"

exit 1

fi

. cfConfigFile.sh

. ilogger.sh

cfInit ${ExpConf} noLock readOnly

if [ $? -ne 0 ];then

echo "Init config error!"

exit 1

fi

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

LogDir=$(cfGetConfigItem common LogDir)

ArchDir=$(cfGetConfigItem common ArchDir)

ExpLogDir=$(cfGetConfigItem common DBLogDir)

cdr_part_exp.conf

%common

LogDir=/opt/mcb/ips/var/log/

ArchDir=/opt/mcb/ips/arch/DB/

DBLogDir=/opt/mcb/ips/var/log/dblogs

26. ps检查进程实例

CheckInst()

{

binName=$1

instName=$2

ps -e|grep " $binName$"|while read pid xx1 xx2

do

ps -f -p $pid|grep "$instName"|grep -v "grep"|wc -l|read icnt

if [ "$icnt" -ne "0" ];then

lgWriteLog 1 CtrlCenter 00000000 "异步入库应用服务的实例[$InstName]进程正在运行!"

echo "异步入库应用服务的实例[$InstName]进程正在运行!"

return 0

fi

done

return 1

}

27. shell编写存储过程

TmpFile=$TmpFilePrefix$CleanDur.lst

sqlplus -S /nolog 2>/dev/null << !!!

connect $dbuser/$dbpwd@$dbsid

spool $TmpFilePath/$TmpFile

DECLARE

/* clean_type D:daily W:weekly M:monthly Y:year */

clean_type VARCHAR2(3) := '$TableType';

clean_time DATE;

BEGIN

LOOP

IF clean_type = 'D' THEN

clean_time := trunc(sysdate-($CleanCycle),'dd');

DELETE FROM $CleanTable

WHERE $CleanTableColumn < clean_time AND rownum<5000;

END IF;

IF sql%notfound THEN

exit;

END IF;

COMMIT;

END LOOP;

END;

/

quit

!!!

28. 数据库连接SQL语句

sqlplus -s "${dbuser}/${dbpasswd}@${dbsid}" <<-! >$logtmp1

create table ${inst_name}_tmp_table as select * from ${dbuser}.${dup_tab_nm} partition($part_name);

insert into ${dup_all_dbuser}.${dup_all_tab_nm}@${dup_all_dblink} select * from ${inst_name}_tmp_table ;

commit;

!

sqlplus -s "$dbuser"/"$dbpasswd"@"$dbsid" <<-EOF >$lv_tmpfile 2>&1

insert into $dbuser.fileval_err_info ( ic_file_nm, file_type, prov_cd, pcs_recv_tm, err_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;

exit

EOF

29. rcp远程操作

errmsg=`rcp ${outfile} ${sz_ics_host}:${sz_ics_dir}/IntFtamFtp.conf`

if [ $? != 0 ];then

lgWriteLog "ALARM" "" -1 "rcp errmsg [$errmsg]"

else

lgWriteLog "SUCC" "" 0 "rcp [%s] to [%s] successfully." "$outfile" "$sz_ics_host:$sz_ics_dir/IntFtamFtp.conf"

fi

30. 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=10240000 log=${log_file} tables=${tablelist} grants=n indexes=n triggers=n

33. 数据库引用sql操作

echo "commit;" >>$tmpsqlpath/insert.sql

echo "spool $reportlst" >>$tmpsqlpath/insert.sql

echo "/" >>$tmpsqlpath/insert.sql

echo "spool off" >>$tmpsqlpath/insert.sql

sqlplus -S /nolog </dev/null 2>&1

connect $sv_userid/$sv_password@int1

@$tmpsqlpath/insert

EOF

************************************practise shell*****************************************

*******

授权命令:chmod 4+2+1 /opt/mcb/ips/myfile

4代表可读,2代表可写,1代表可执行

授权目录:chmod -R 4+2+1 /opt/mcb/wbs

转移用户文件归属:chown filename owner

转移用户目录归属:chown -R dir owner

转移用户组文件归属:chgrp filename groupA

转移用户组目录归属:chgrp -R dir groupB

find命令:find . -name "*.txt" --查找当前目录下面的文本文件

find /opt/mcb/wbs -name "*.txt" --查找指定目录下面的文本文件

find /opt/mcb/wbs -type f -name "*.txt"

-type 查找某一类型的文件,诸如:

b - 块设备文件。

d - 目录。

c - 字符设备文件。

p - 管道文件。

l - 符号链接文件。

f - 普通文件。

find /opt/mcb/wbs -type f -name "*.log" -exec rm {} \\; --找出后执行命令

查看进程命令:ps -ef | grep "java"

杀进程命令:kill 28679

用户推出不挂起操作:nohup command

控制台输入读取:echo -n "Can I clear your saved files(y/n):"

read answer

echo "your choise is "$answer

重定向输出:echo "Hello World In Java" >> abc.txt

转义字符:echo "\\"/dev/rmt0"\\"

cat命令:cat passwd |awk -F: '{print $1" and "$2" over all shutdown...

出4

myarr[1]=AD2

myarr[2]=KP9

myarr[3]=JU2

myarr[4]=LP

substr(s,p) 返回字符串s中从p开始的后缀部分

substr(s,p,n) 返回字符串s中从p开始长度为n的后缀部分

awk '{print substr("abc123teckwon

命令B

done

for fruit in "apple orange pear watermelon mengo"

echo $fruit

done

counter=0

for files in `ls /opt/mcb/ips/conf`

do

echo $files

counter=`expr counter+1`

done

echo "There is $counter files in total."

until命令:循环至少执行一次

until 条件表达式

do

命令A

命令B

done

lock_file=/tmp/process.lck

until [ ! -f $lock_file ]

do

echo "lock is on the way..please wait for 5 second..."

sleep 5

done

while 条件表达式

do

命令A

命令B

done

counter=0

while [ $counter -lt 5 ]

do

counter=`expr counter + 1`

echo "$counter"

done

echo "The counter is already done in 5..."

cat files | while read rowline

do

命令A

命令B

done

文档

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_pattern="[AC][CD]?CHNCM*"db_use
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top