Shell脚本抽取实例


#!/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 ["\n"] > $matchfile
##^M替换为换行
cat $file | tr -s "[\015]" "\n" > $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..."}'

正则表达式:^[A-Z] --以大写字母打头
^[A-Za-z] --以字母打头
^[0-9] --以数字打头
^[A-Za-z0-9] --以数字或者字母打头
trouble$ --以trouble结尾
A\{2\}B --匹配字母A出现两次
A\{4,\}B --匹配字母A最少出现四次
A\{2,4\}B --匹配字母A出现2到4次
[^0-9A-Za-z] --非数字和字母打头
[Cc]omputer --匹配Computer或者computer

grep命令:grep -i abc myfile --匹配abc字符串,不区分大小写
grep -n abc myfile --输出匹配abc字符串信息,并打印出序号
grep -n '^$' myfile --输出空行序号

awk命令:cat myfile | awk -F: '{print "rowline"NR" "$1" and "$2}' | read abstr --NR代表序号
echo $abstr

awk内置字符串函数:gsub(r,s) 在整个$ 0中用s替代r
awk 'gsub(\4849\,1225) {print $0}' grade.txt
index(s,t) 返回s中字符串t的第一位置
awk '{print index("teckwon831227","8")}' grade.txt
length(s) 返回s长度
awk -F: '{print length($3)}' grade.txt
match(s,r) 测试s是否包含匹配r的字符串
awk '{print match($3,"abc")}' grade.txt
split(str,arr,fs) 以fs作为分割符将str字符串分成数组arr,并返回下标
awk '{print split(AD2-KP9-JU2-LP1,myarr,"-")}'
echo $? --输

出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","123",4)}' --输出teck

sort命令:video.txt:
teckwon:HK:23:12580
fanyuan:CH:21:89654
Alian:FR:30:87555
pangpang:EN:20:80000

field0 field1 field2 field3
teckwon HK 23 12580
fanyuan CH 21 89654

sort -t: video.txt --按照:作为分隔符,默认按照field0进行升序排序
sort -t: -r video.txt --按照降序进行排序
sort -t: +1 video.txt --按照field1进行升序排序
sort -t: +2n video.txt --数值排序要加上n,然后进行field2数值升序排序
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 --只显示每一重复行出现次数

tr命令:
oop.txt:And the cowwwwwwwws went homeeeeeeeee
cat oop.txt | tr -s "[a-z]" --删除每一行重复出现的字母
输出:And the cows went home
tr -s "[\012]" < plane.txt --删除空行
cat plane.txt | tr "[a-z]" "[A-Z]" --转换从小写到大写
tr -s "[\015]" "\n" < plane.txt --将所有^M转换成回车

set命令显示本地所有Shell变量
unset命令释放变量
bdf命令显示所有磁盘空间大小

echo $? --读出程序运营结果
echo $# --读出传入参数个数
exit 0 --程序执行成功退出
exit 1 --程序执行失败退出

循环控制流命令:
if [条件表达式A]; then
elif [条件表达式B]; then
else
fi

dir=$1
if ["`ls -A $dir`" = ""]; then
echo "the input directory is indeed empty!!!"
exit 1
else
echo "directory complete!!!"
fi

case 值 in
模式A)
命令A
;;
模式B)
命令B
;;
*)
执行默认命令
exit 1
;;
esac

echo -n "choices are .. vt100, vt102, vt220. Enter your terminal type:"
read choice
result="error"
case $choice in
vt100|vt102)
result=vt100
;;
vt220)
result=vt220
;;
*)
echo "your input is out of selection, reuslt will be set to be error."
result=error
exit 1
;;
esac

for 变量 in 列表
do
命令A

命令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
























相关主题
相关文档
最新文档