- 浏览: 99883 次
- 性别:
- 来自: 武汉
文章分类
最新评论
-
yaerfeng1989:
jbpm代码下载:http://www.zuidaima.co ...
jbpm -
history1918:
密码在哪里?
JAVA虚拟机运行时分析 -
chunecai:
同上,感谢。
ext api chm -
zqb666kkk:
楼主下载 的资源 早起的版本 跟后来的版本 怎么都放出来啊 ...
ssh -
ericFang:
ONE:XSD和WSDL接口定义
XSD文件主要是定义请 ...
WebServices axis
linux shell
- Unix.Shell编程_第三版_.part1.rar (10 MB)
- 下载次数: 26
- Unix.Shell编程_第三版_.part2.rar (9.6 MB)
- 下载次数: 26
- Linux_shell.pdf (1.8 MB)
- 下载次数: 44
评论
19 楼
ericFang
2011-03-30
› 如果脚本文件的第一个字符不是“#”号,则启动bsh执行此脚本
› 如果脚本文件的第一个字符是“#”号,但第二个字符不是“!”,则启动csh执行此脚本
› 如果脚本文件的前两个字符为“#!”,则后面必须是Shell的全路径,启动指定的Shell执行此脚本,
$0 当前脚本的名字
$1-9 位置参量1~9
${10} 位置参量10
$# 位置参量的个数
$* 所有的位置参量
$@ 同上
“$*” 扩展为一个
“$@” 扩展为多个
› 如果脚本文件的第一个字符是“#”号,但第二个字符不是“!”,则启动csh执行此脚本
› 如果脚本文件的前两个字符为“#!”,则后面必须是Shell的全路径,启动指定的Shell执行此脚本,
$0 当前脚本的名字
$1-9 位置参量1~9
${10} 位置参量10
$# 位置参量的个数
$* 所有的位置参量
$@ 同上
“$*” 扩展为一个
“$@” 扩展为多个
18 楼
ericFang
2011-03-30
命令行的处理顺序:
历史记录替换(需要设置)
命令行拆分为Token(以空格和制表符分隔)
历史引用
处理引用
定义别名替换和函数(如果需要)
建立重新定向、后台和管道
变量替换($user等)
命令替换(echo for today is `date`)
文件名替换
程序执行(内建命令直接执行,否则在PATH变量指定的目录列表中查找)
当Shell准备好执行命令的时候,按如下命令类型顺序执行(type):
别名
关键字
函数
内建命令
可执行程序
历史记录替换(需要设置)
命令行拆分为Token(以空格和制表符分隔)
历史引用
处理引用
定义别名替换和函数(如果需要)
建立重新定向、后台和管道
变量替换($user等)
命令替换(echo for today is `date`)
文件名替换
程序执行(内建命令直接执行,否则在PATH变量指定的目录列表中查找)
当Shell准备好执行命令的时候,按如下命令类型顺序执行(type):
别名
关键字
函数
内建命令
可执行程序
17 楼
ericFang
2011-03-30
< 重新定向输入($ tr ‘[A-Z]’ ‘[a-z]’ < file) > 重新定向输出($ ls > file) >> 追加输出($ date >> file) 2> 重新定向错误($ cc prog.c 2> errfile) &>(>&) 重新定向错误和标准输出($ find . -name “*.c” -print >& file) 2>&1 重新定向错误到标准输出($ find . > file 2>&1)
16 楼
ericFang
2010-12-01
http://linux.chinaunix.net/bbs/thread-1174532-1-1.html
15 楼
ericFang
2010-09-27
远程执行命令:
`ssh_run.sh $1 user password "cmd"|grep rac|xargs`
ssh_run.sh:
`ssh_run.sh $1 user password "cmd"|grep rac|xargs`
ssh_run.sh:
#!/usr/bin/expect if {$argc!=4 && $argc!=5} { send_user "Usage: $argv0 {host} {username} {password} {cmdline} {timeout}\n\n" exit 1 } set HOST [lindex $argv 0] set USER [lindex $argv 1] set PASSWD [lindex $argv 2] set CMD [lindex $argv 3] set TIMEOUT [lindex $argv 4] set done 0 if { $TIMEOUT == "" } { set TIMEOUT 15 } proc remove_known_hosts_entry {host} { regsub ".*/" $::argv0 "" name set tmp_file "/tmp/$name.tmp" set known_hosts "$::env(HOME)/.ssh/known_hosts" if {[catch { set fd_known_hosts [open $known_hosts r] set fdTmp [open $tmp_file w] while 1 { gets $fd_known_hosts line if [eof $fd_known_hosts] { break } if [regexp "(\[^, ]+,)*${host}(,\[^, ]+)* " $line] { continue } puts $fdTmp $line } close $fd_known_hosts close $fdTmp file rename -force $tmp_file $known_hosts } error]} { send_user "$error\n" exit 2 } } set timeout ${TIMEOUT} spawn -noecho ssh ${USER}@${HOST} "$CMD" while { 1 } { expect { timeout { exit 4 } -nocase -re "Permission denied*" { exit 3 } "@@@@@@@@@@@@@@@@@@@@" { remove_known_hosts_entry $HOST spawn -noecho ssh ${USER}@${HOST} "$CMD" } -nocase -re "yes/no" { send "yes\n" } -nocase -re "password: " { send "${PASSWD}\n" } -nocase "try again" { exit 3 } eof { exit 0 } } }
14 楼
ericFang
2010-08-18
#!/usr/bin/expect spawn ssh -q 172.16.128.0 set timeout 600 expect { timeout {exit 2} -nocase -re "Password:" {send "huawei\r"} } expect { -nocase -re "password:" {exit 3} "omu*" } send "rm -r /opt/output\r" send "exit\r" spawn scp -r [lindex $argv 0] 172.16.128.0:/opt expect "*Password:" send "huawei\n" expect "fdsfds" send "exit\r"
13 楼
ericFang
2010-08-12
function echo_OK () { echo_attention "INFO" "OK" } function echo_FAIL () { echo_attention "ERROR" "FAIL" } function rollback_data() { truncate_table="sqlplus -S dbo/huawei <<! 2>/dev/null set head off TRUNCATE TABLE T_PCC_SUBSCRIBER; commit; quit !" truncate_table_result=$(su - oracle -c "${truncate_table}") echo ${truncate_table_result}>>${log_file} exe_roll="imp dbo/huawei file=${bak_file_dmp} full=y ignore=y" local TMP_SH_FILE=/opt/UpdateMsisdnTool/AA.SH echo ${exe_roll} > ${TMP_SH_FILE} su - oracle -c "bash ${TMP_SH_FILE}">${log_file} 2>&1 #roll_result=$(su - oracle -c "${exe_roll}") return 0 }
12 楼
ericFang
2010-08-11
function echo_step() { if [ "$1" = "" ] || [ "$2" = "" ] then echo "$@" return fi stepStr=$2 stepStr="${stepStr}"" ............................................. " echolen=`tput cols` 2>/dev/null if [ "${echolen}" = "" ] then echolen=45 elif [ ${echolen} -gt 75 ] then echolen=58 elif [ ${echolen} -lt 15 ] then echolen=10 else let echolen=echolen-10 fi stty echo 2>/dev/null case "$1" in "1") stepStr=${stepStr:0:$echolen} step_num=`expr ${step_num} + 1` if [ ${step_num} -le 9 ] then step_num="0${step_num}" fi new_sub_step_flag=1 echo -e -n "[`date +%H:%M:%S`]NOW${step_num}: ${stepStr}" ;; "2") if [ $new_sub_step_flag -eq 1 ] then substep_num=0 new_sub_step_flag=0 fi let echolen=echolen-10 stepStr=${stepStr:0:$echolen} substep_num=`expr $substep_num + 1` if [ ${substep_num} -le 9 ] then step_num="0${substep_num}" fi echo -n "[`date +%H:%M:%S`] Step${step_num}.${substep_num}: ${stepStr}" ;; "reset") step_num=0 ;; *) echo -n "$@" ;; esac }
11 楼
ericFang
2010-08-11
function echo_attention () { stty echo 2>/dev/null if [ "${OKI_FLAG}" = "1" ]; then return ${SUCC} fi if [ -z "$1" -o -z "$2" -o "$#" -ne 2 ]; then echo "$1" return ${SUCC} fi case "$1" in "INFO") echo -e "\033[40;32m$2\033[40;37m" ;; "WARNING") echo -e "\033[40;33m$2\033[40;37m" ;; "ERROR") echo -e "\033[40;31m$2\033[40;37m" ;; "PROMPT") echo -e -n "Please input $2: " ;; *) echo -e "$@" ;; esac return ${SUCC} }
10 楼
ericFang
2010-06-12
sed
http://bbs.chinaunix.net/viewthread.php?tid=605565
http://bbs.chinaunix.net/viewthread.php?tid=605565
9 楼
ericFang
2010-06-09
#!/bin/bash
backup_conf()
{
prtmsg -nprt "Backup config files!"
cp /etc/init.d/rm9000sys ${CONF}/conf_bak/rm9000sys
cp /opt/RM9000/autorun/S20RM9000MONITOR.sh ${CONF}/conf_bak/S20RM9000MONITOR.sh > /dev/null 2>&1
cp ${RM9000_PMS}/pms/conf/dmf/treecache.xml ${CONF}/conf_bak/treecache.xml
cp ${RM9000_PMS}/pms/conf/dmf/hibernate.cfg.xml ${CONF}/conf_bak/hibernate.cfg.xml
cp ${RM9000_PMS}/pms/jboss-4.0.5.GA/server/rm9000/deploy/cluster-service.xml ${CONF}/conf_bak/cluster-service.xml
cp ${RM9000_PMS}/pms/conf/maf/maf-mbean-config.xml ${CONF}/conf_bak/maf-mbean-config.xml
}
copy_conf()
{
prtmsg -nprt "Copy config files to server from $1"
cp $1/rm9000sys /etc/init.d/rm9000sys
cp $1/treecache.xml ${RM9000_PMS}/pms/conf/dmf/treecache.xml
cp $1/hibernate.cfg.xml ${RM9000_PMS}/pms/conf/dmf/hibernate.cfg.xml
cp $1/cluster-service.xml ${RM9000_PMS}/pms/jboss-4.0.5.GA/server/rm9000/deploy/cluster-service.xml
cp $1/maf-mbean-config.xml ${RM9000_PMS}/pms/conf/maf/maf-mbean-config.xml
}
creat_new_conf()
{
if [ -z "`cat ${RM9000_PMS}/pms/conf/dmf/treecache.xml|grep FD_SIMPLE`" ]
then
sed '88i<FD_SIMPLE timeout="2000" max_missed_hbs="5"/>' ${RM9000_PMS}/pms/conf/dmf/treecache.xml| sed 's/shun="true"/shun="false"/' | sed 's/48866/998/' > ${CONF}/treecache.xml
else
sed 's/shun="true"/shun="false"/' ${RM9000_PMS}/pms/conf/dmf/treecache.xml|sed 's/48866/998/' > ${CONF}/treecache.xml
fi
if [ -z "`cat ${RM9000_PMS}/pms/conf/dmf/hibernate.cfg.xml|grep dbconnection.logintimeout`" ]
then
sed '20i<property name="dbconnection.logintimeout">10</property>' ${RM9000_PMS}/pms/conf/dmf/hibernate.cfg.xml | sed 's/"c3p0.timeout">0/"c3p0.timeout">120/' | sed 's/c3p0.max_statements">1000/c3p0.max_statements">0/' > ${CONF}/hibernate.cfg.xml
else
sed 's/"c3p0.timeout">0/"c3p0.timeout">120/' ${RM9000_PMS}/pms/conf/dmf/hibernate.cfg.xml | sed 's/c3p0.max_statements">1000/c3p0.max_statements">0/' > ${CONF}/hibernate.cfg.xml
fi
sed 's/45566/999/' ${RM9000_PMS}/pms/jboss-4.0.5.GA/server/rm9000/deploy/cluster-service.xml|sed 's/shun="true"/shun="false"/' > ${CONF}/cluster-service.xml
sed -e 's/od>[0-9]*<\/db/od>5000<\/db/' ${RM9000_PMS}/pms/conf/maf/maf-mbean-config.xml > ${CONF}/maf-mbean-config.xml
}
conf_main()
{
if [ "${OPSTYLE}" = "upgrade" ]
then
backup_conf
creat_new_conf
copy_conf "${CONF}"
cp ${CONF}/timezone /opt/RM9000/.timezone
rm -f /opt/RM9000/autorun/S20RM9000MONITOR.sh
pids=`ps -ef|grep bash|grep Monitor_Rm9000.sh|awk '{print $2}'`
for pid in $pids;
do
kill -9 $pid >/dev/null 2>&1
done
rm -f -r /opt/RM9000/pms/pms/jboss-4.0.5.GA/server/rm9000/deploy/snmp-adaptor.sar
rm -f /etc/init.d/rc5.d/*rm9000sys
rm -f /etc/init.d/rc3.d/*rm9000sys
prtmsg -nprt $(chkconfig -a rm9000sys)
elif [ "${OPSTYLE}" = "rollback" ]
then
rm -f /opt/RM9000/.timezone
#cp ${CONF}/conf_back/S20RM9000MONITOR.sh /opt/RM9000/autorun/S20RM9000MONITOR.sh
#bash /opt/RM9000/autorun/S20RM9000MONITOR.sh
copy_conf "${CONF}/conf_bak"
fi
}
conf_main
backup_conf()
{
prtmsg -nprt "Backup config files!"
cp /etc/init.d/rm9000sys ${CONF}/conf_bak/rm9000sys
cp /opt/RM9000/autorun/S20RM9000MONITOR.sh ${CONF}/conf_bak/S20RM9000MONITOR.sh > /dev/null 2>&1
cp ${RM9000_PMS}/pms/conf/dmf/treecache.xml ${CONF}/conf_bak/treecache.xml
cp ${RM9000_PMS}/pms/conf/dmf/hibernate.cfg.xml ${CONF}/conf_bak/hibernate.cfg.xml
cp ${RM9000_PMS}/pms/jboss-4.0.5.GA/server/rm9000/deploy/cluster-service.xml ${CONF}/conf_bak/cluster-service.xml
cp ${RM9000_PMS}/pms/conf/maf/maf-mbean-config.xml ${CONF}/conf_bak/maf-mbean-config.xml
}
copy_conf()
{
prtmsg -nprt "Copy config files to server from $1"
cp $1/rm9000sys /etc/init.d/rm9000sys
cp $1/treecache.xml ${RM9000_PMS}/pms/conf/dmf/treecache.xml
cp $1/hibernate.cfg.xml ${RM9000_PMS}/pms/conf/dmf/hibernate.cfg.xml
cp $1/cluster-service.xml ${RM9000_PMS}/pms/jboss-4.0.5.GA/server/rm9000/deploy/cluster-service.xml
cp $1/maf-mbean-config.xml ${RM9000_PMS}/pms/conf/maf/maf-mbean-config.xml
}
creat_new_conf()
{
if [ -z "`cat ${RM9000_PMS}/pms/conf/dmf/treecache.xml|grep FD_SIMPLE`" ]
then
sed '88i<FD_SIMPLE timeout="2000" max_missed_hbs="5"/>' ${RM9000_PMS}/pms/conf/dmf/treecache.xml| sed 's/shun="true"/shun="false"/' | sed 's/48866/998/' > ${CONF}/treecache.xml
else
sed 's/shun="true"/shun="false"/' ${RM9000_PMS}/pms/conf/dmf/treecache.xml|sed 's/48866/998/' > ${CONF}/treecache.xml
fi
if [ -z "`cat ${RM9000_PMS}/pms/conf/dmf/hibernate.cfg.xml|grep dbconnection.logintimeout`" ]
then
sed '20i<property name="dbconnection.logintimeout">10</property>' ${RM9000_PMS}/pms/conf/dmf/hibernate.cfg.xml | sed 's/"c3p0.timeout">0/"c3p0.timeout">120/' | sed 's/c3p0.max_statements">1000/c3p0.max_statements">0/' > ${CONF}/hibernate.cfg.xml
else
sed 's/"c3p0.timeout">0/"c3p0.timeout">120/' ${RM9000_PMS}/pms/conf/dmf/hibernate.cfg.xml | sed 's/c3p0.max_statements">1000/c3p0.max_statements">0/' > ${CONF}/hibernate.cfg.xml
fi
sed 's/45566/999/' ${RM9000_PMS}/pms/jboss-4.0.5.GA/server/rm9000/deploy/cluster-service.xml|sed 's/shun="true"/shun="false"/' > ${CONF}/cluster-service.xml
sed -e 's/od>[0-9]*<\/db/od>5000<\/db/' ${RM9000_PMS}/pms/conf/maf/maf-mbean-config.xml > ${CONF}/maf-mbean-config.xml
}
conf_main()
{
if [ "${OPSTYLE}" = "upgrade" ]
then
backup_conf
creat_new_conf
copy_conf "${CONF}"
cp ${CONF}/timezone /opt/RM9000/.timezone
rm -f /opt/RM9000/autorun/S20RM9000MONITOR.sh
pids=`ps -ef|grep bash|grep Monitor_Rm9000.sh|awk '{print $2}'`
for pid in $pids;
do
kill -9 $pid >/dev/null 2>&1
done
rm -f -r /opt/RM9000/pms/pms/jboss-4.0.5.GA/server/rm9000/deploy/snmp-adaptor.sar
rm -f /etc/init.d/rc5.d/*rm9000sys
rm -f /etc/init.d/rc3.d/*rm9000sys
prtmsg -nprt $(chkconfig -a rm9000sys)
elif [ "${OPSTYLE}" = "rollback" ]
then
rm -f /opt/RM9000/.timezone
#cp ${CONF}/conf_back/S20RM9000MONITOR.sh /opt/RM9000/autorun/S20RM9000MONITOR.sh
#bash /opt/RM9000/autorun/S20RM9000MONITOR.sh
copy_conf "${CONF}/conf_bak"
fi
}
conf_main
8 楼
ericFang
2010-06-09
tools.sh
#!/bin/bash
do_d2u()
{
echo " start to convert files type from dos to unix"
for f in `find $1 -print | grep -v 'backup' | grep '\.sh$\|\.[sc][qf][lg]$'`
do
if [ -f "$f" ]
then
if [ `grep -c '
$' "$f"` -eq 0 ]
then
return
fi
dos2unix "$f"
fi
done
echo " finished"
}
clearDir()
{
for f in $(find $1 -print )
do
if [ -d $f ]; then
continue
fi
rm -vf $f
done
}
rm_emp_dir()
{
for f in $(find $1 -print | sort -r)
do
if [ -d $f ]; then
rmdir -v $f
fi
done
}
main()
{
if [ $# -ne 2 ];
then
echo "para is error"
exit
fi
opcode=$1
case "$opcode" in
"-d2u")
d2u $2
;;
"-clear")
clearDir $2
;;
"-rm")
rm_emp_dir $2
;;
*)
echo "operaction is not supported!"
;;
esac
}
main $@
#!/bin/bash
do_d2u()
{
echo " start to convert files type from dos to unix"
for f in `find $1 -print | grep -v 'backup' | grep '\.sh$\|\.[sc][qf][lg]$'`
do
if [ -f "$f" ]
then
if [ `grep -c '
$' "$f"` -eq 0 ]
then
return
fi
dos2unix "$f"
fi
done
echo " finished"
}
clearDir()
{
for f in $(find $1 -print )
do
if [ -d $f ]; then
continue
fi
rm -vf $f
done
}
rm_emp_dir()
{
for f in $(find $1 -print | sort -r)
do
if [ -d $f ]; then
rmdir -v $f
fi
done
}
main()
{
if [ $# -ne 2 ];
then
echo "para is error"
exit
fi
opcode=$1
case "$opcode" in
"-d2u")
d2u $2
;;
"-clear")
clearDir $2
;;
"-rm")
rm_emp_dir $2
;;
*)
echo "operaction is not supported!"
;;
esac
}
main $@
7 楼
ericFang
2010-05-28
prtmsg()
{
colorflg=0 # color display
while [ x"$1" != x ]; do
case "$1" in
-nprt) colorflg=-1; logflg=0
shift
;;
-log) colorflg=-1; logflg=1
shift
;;
-info) if [ $colorflg -eq 0 ]; then colorflg=1; logflg=1; fi
shift
;;
-error) if [ $colorflg -eq 0 ]; then colorflg=3; logflg=1; fi
shift
;;
-warn) if [ $colorflg -eq 0 ]; then colorflg=2; logflg=1; fi
shift
;;
*) msg="$@"
break
;;
esac
done
case $colorflg in
1) echo -n -e "\033[40;32m";; # green color
2) echo -n -e "\033[40;33m";; # yellow color
3) echo -n -e "\033[40;31m";; # red color
*) ;;
esac
if [ $logflg -eq 1 ]; then
printf "$msg \n"
fi
if [ $colorflg -ge 0 ]; then
echo -n -e "\033[40;37m"
fi
curdate=`date '+%F %H:%M:%S'`
printf "[$curdate] " >>${LOG}/detail.log
printf "$msg \n" >>${LOG}/detail.log
}
{
colorflg=0 # color display
while [ x"$1" != x ]; do
case "$1" in
-nprt) colorflg=-1; logflg=0
shift
;;
-log) colorflg=-1; logflg=1
shift
;;
-info) if [ $colorflg -eq 0 ]; then colorflg=1; logflg=1; fi
shift
;;
-error) if [ $colorflg -eq 0 ]; then colorflg=3; logflg=1; fi
shift
;;
-warn) if [ $colorflg -eq 0 ]; then colorflg=2; logflg=1; fi
shift
;;
*) msg="$@"
break
;;
esac
done
case $colorflg in
1) echo -n -e "\033[40;32m";; # green color
2) echo -n -e "\033[40;33m";; # yellow color
3) echo -n -e "\033[40;31m";; # red color
*) ;;
esac
if [ $logflg -eq 1 ]; then
printf "$msg \n"
fi
if [ $colorflg -ge 0 ]; then
echo -n -e "\033[40;37m"
fi
curdate=`date '+%F %H:%M:%S'`
printf "[$curdate] " >>${LOG}/detail.log
printf "$msg \n" >>${LOG}/detail.log
}
6 楼
ericFang
2010-05-27
netstat -ant|grep 5566
netstat -adu 查看UPD端口的监听
dos2unix 功能是将DOS/MAC下的文件转化为UNIX的文本文件格式
cat ${file_name}|awk -F : '{print $2}' 查出指定文件中以:分隔的行的第二列的内容
grep password ${file_name}|cut -d: -f2 查出指定文件中含password的行并以:分隔的第二列的内容
ping ${ip} -c 1 -w 1 > /dev/null 2>&1 :ping 命令只执行1秒,并将输出的打印输出到垃圾箱
find ${FILE_PATH} -type f -mtime +${duration} -exec rm {} \;
find /opt/export_apn/output -type f -mtime +1
lsof -i:1521 | grep rm
netstat -adu 查看UPD端口的监听
dos2unix 功能是将DOS/MAC下的文件转化为UNIX的文本文件格式
cat ${file_name}|awk -F : '{print $2}' 查出指定文件中以:分隔的行的第二列的内容
grep password ${file_name}|cut -d: -f2 查出指定文件中含password的行并以:分隔的第二列的内容
ping ${ip} -c 1 -w 1 > /dev/null 2>&1 :ping 命令只执行1秒,并将输出的打印输出到垃圾箱
find ${FILE_PATH} -type f -mtime +${duration} -exec rm {} \;
find /opt/export_apn/output -type f -mtime +1
lsof -i:1521 | grep rm
5 楼
ericFang
2010-05-21
export.conf配置文件参数的说明:
参数 意义 取值范围 备注
第一列 分钟 1-59 *表示每分钟都执行导出任务
第二列 小时 1-23 0表示子夜,*表示每小时都执行导出任务
第三列 日 1-31 *表示每天都执行导出任务
第四列 月 1-12 *表示每月都执行导出任务
第五列 星期 0-6 0表示星期天,*表示每星期都执行导出任务
注意:请不要修改第六列SCRIPT,并将参数以空格隔开,建议配置在业务量较低的时间执行。
apn.conf配置范例:
ip:8.1.240.36
user:eric
password:eric
directory:file
duration:6
apn.conf配置范例解释:
ip:8.1.240.36 --表示FTP服务器的IP为8.1.240.36
user:eric --表示登录FTP服务器的用户名为eric
password:eric --表示登录FTP服务器的用户密码为eric
directory:file --表示生成的文件上传到FTP服务器的file目录下
duration:6 --表示PDU单板上生成的文件只保留6天以内的文件
参数 意义 取值范围 备注
第一列 分钟 1-59 *表示每分钟都执行导出任务
第二列 小时 1-23 0表示子夜,*表示每小时都执行导出任务
第三列 日 1-31 *表示每天都执行导出任务
第四列 月 1-12 *表示每月都执行导出任务
第五列 星期 0-6 0表示星期天,*表示每星期都执行导出任务
注意:请不要修改第六列SCRIPT,并将参数以空格隔开,建议配置在业务量较低的时间执行。
apn.conf配置范例:
ip:8.1.240.36
user:eric
password:eric
directory:file
duration:6
apn.conf配置范例解释:
ip:8.1.240.36 --表示FTP服务器的IP为8.1.240.36
user:eric --表示登录FTP服务器的用户名为eric
password:eric --表示登录FTP服务器的用户密码为eric
directory:file --表示生成的文件上传到FTP服务器的file目录下
duration:6 --表示PDU单板上生成的文件只保留6天以内的文件
4 楼
ericFang
2010-05-21
目录结构如下(可执行tree命令查看):
|-- apn_sub.sh
|-- conf
| |-- apn.conf
| `-- export.conf
|-- log
|-- output
|-- run.sh
|-- upload.sh
apn_sub.sh 主要实现从数据库导出数据并写到文件
conf目录存储的是配置文件
apn.conf存储ftp服务器相关信息和文件保留天数
export.conf 存储定时任务的执行时间
log目录存储工具运行的日志文件
output目录存储生成的文件
run.sh主要实现让工具定时执行功能
upload.sh主要实现将output目录下生成的文件上传到指定的ftp服务器目录下
|-- apn_sub.sh
|-- conf
| |-- apn.conf
| `-- export.conf
|-- log
|-- output
|-- run.sh
|-- upload.sh
apn_sub.sh 主要实现从数据库导出数据并写到文件
conf目录存储的是配置文件
apn.conf存储ftp服务器相关信息和文件保留天数
export.conf 存储定时任务的执行时间
log目录存储工具运行的日志文件
output目录存储生成的文件
run.sh主要实现让工具定时执行功能
upload.sh主要实现将output目录下生成的文件上传到指定的ftp服务器目录下
3 楼
ericFang
2010-05-21
upload.sh
#!/bin/bash
function def_global_var()
{
SUCC=0
FAIL=1
WORK_DIR=/opt/export_apn
OUTPUT_DIR=${WORK_DIR}/output
LOG_DIR=${WORK_DIR}/log
CONF_FILE=${WORK_DIR}/conf/export.conf
LOG_FILE=${LOG_DIR}/export.log
>${LOG_FILE}
EXPORT_SCRIPT=apn_sub.sh
return ${SUCC}
}
function base_check()
{
if [ "$(whoami)" != "root" ]; then
echo_log "Current user must be 'root'"
exit ${FAIL}
fi
current_patition=opt
remain_space=`df|grep "$current_patition" | awk {'print $4'}`
if [ ${remain_space} -lt 1000000 ];then
echo_log "There is no enough space for data export on patiton ${current_patition}."
return ${FAIL}
> ${WORK_DIR}/conf/export.conf.bak
> ${WORK_DIR}/conf/export.conf.tmp
crontab -u root -l | grep -v "^#" > ${WORK_DIR}/conf/export.conf.bak 2>&1
tag=`cat ${WORK_DIR}/conf/export.conf.bak | grep -v "^#" | grep -ic "${EXPORT_SCRIPT}"`
if [ ${tag} -ge 1 ];then
sed '/'${EXPORT_SCRIPT}'/d' ${WORK_DIR}/conf/export.conf.bak > ${WORK_DIR}/conf/export.conf.tmp
else
cat ${WORK_DIR}/conf/export.conf.bak > ${WORK_DIR}/conf/export.conf.tmp
fi
crontab -u root ${WORK_DIR}/conf/export.conf.tmp > ${LOG_FILE} 2>&1
result=$?
if [ ${result} != ${SUCC} ];then
echo_log "Remove crontab fail."
return ${FAIL}
fi
echo_log "The registration for data export has been removed."
exit 0
fi
return ${SUCC}
}
function echo_log()
{
local date_time=`date '+%Y%m%d:%H%M%S'`
# echo "$1"
echo "&{date_time} $1" >> ${LOG_FILE}
return ${SUCC}
}
function regist_export()
{
if [ ! -f ${CONF_FILE} ];then
echo_log "The configuration file ${CONF_FILE} for registration is not existed."
return ${FAIL}
fi
> ${WORK_DIR}/conf/export.conf.bak
> ${WORK_DIR}/conf/export.conf.tmp
crontab -u root -l | grep -v "^#" > ${WORK_DIR}/conf/export.conf.bak 2>&1
tag=`cat ${WORK_DIR}/conf/export.conf.bak | grep -v "^#" | grep -ic "${EXPORT_SCRIPT}"`
if [ ${tag} -ge 1 ];then
sed '/'${EXPORT_SCRIPT}'/d' ${WORK_DIR}/conf/export.conf.bak > ${WORK_DIR}/conf/export.conf.tmp
else
cat ${WORK_DIR}/conf/export.conf.bak > ${WORK_DIR}/conf/export.conf.tmp
fi
sed "s|SCRIPT|${WORK_DIR}/${EXPORT_SCRIPT} |" ${WORK_DIR}/conf/export.conf >> ${WORK_DIR}/conf/export.conf.tmp
crontab -u root ${WORK_DIR}/conf/export.conf.tmp > ${LOG_FILE} 2>&1
result=$?
if [ ${result} != ${SUCC} ];then
echo_log "The format of the configuration file ${CONF_FILE} is not correct."
return ${FAIL}
fi
/etc/init.d/cron restart > ${LOG_FILE} 2>&1
echo_log "The data export task has been registrated in your system successfully."
return ${SUCC}
}
function main()
{
def_global_var
base_check
tag=$?
if [ "${tag}" != "0" ]; then
echo_log "callback base_check function return fail"
exit ${tag}
fi
regist_export
return ${SUCC}
}
main $@
#!/bin/bash
function def_global_var()
{
SUCC=0
FAIL=1
WORK_DIR=/opt/export_apn
OUTPUT_DIR=${WORK_DIR}/output
LOG_DIR=${WORK_DIR}/log
CONF_FILE=${WORK_DIR}/conf/export.conf
LOG_FILE=${LOG_DIR}/export.log
>${LOG_FILE}
EXPORT_SCRIPT=apn_sub.sh
return ${SUCC}
}
function base_check()
{
if [ "$(whoami)" != "root" ]; then
echo_log "Current user must be 'root'"
exit ${FAIL}
fi
current_patition=opt
remain_space=`df|grep "$current_patition" | awk {'print $4'}`
if [ ${remain_space} -lt 1000000 ];then
echo_log "There is no enough space for data export on patiton ${current_patition}."
return ${FAIL}
> ${WORK_DIR}/conf/export.conf.bak
> ${WORK_DIR}/conf/export.conf.tmp
crontab -u root -l | grep -v "^#" > ${WORK_DIR}/conf/export.conf.bak 2>&1
tag=`cat ${WORK_DIR}/conf/export.conf.bak | grep -v "^#" | grep -ic "${EXPORT_SCRIPT}"`
if [ ${tag} -ge 1 ];then
sed '/'${EXPORT_SCRIPT}'/d' ${WORK_DIR}/conf/export.conf.bak > ${WORK_DIR}/conf/export.conf.tmp
else
cat ${WORK_DIR}/conf/export.conf.bak > ${WORK_DIR}/conf/export.conf.tmp
fi
crontab -u root ${WORK_DIR}/conf/export.conf.tmp > ${LOG_FILE} 2>&1
result=$?
if [ ${result} != ${SUCC} ];then
echo_log "Remove crontab fail."
return ${FAIL}
fi
echo_log "The registration for data export has been removed."
exit 0
fi
return ${SUCC}
}
function echo_log()
{
local date_time=`date '+%Y%m%d:%H%M%S'`
# echo "$1"
echo "&{date_time} $1" >> ${LOG_FILE}
return ${SUCC}
}
function regist_export()
{
if [ ! -f ${CONF_FILE} ];then
echo_log "The configuration file ${CONF_FILE} for registration is not existed."
return ${FAIL}
fi
> ${WORK_DIR}/conf/export.conf.bak
> ${WORK_DIR}/conf/export.conf.tmp
crontab -u root -l | grep -v "^#" > ${WORK_DIR}/conf/export.conf.bak 2>&1
tag=`cat ${WORK_DIR}/conf/export.conf.bak | grep -v "^#" | grep -ic "${EXPORT_SCRIPT}"`
if [ ${tag} -ge 1 ];then
sed '/'${EXPORT_SCRIPT}'/d' ${WORK_DIR}/conf/export.conf.bak > ${WORK_DIR}/conf/export.conf.tmp
else
cat ${WORK_DIR}/conf/export.conf.bak > ${WORK_DIR}/conf/export.conf.tmp
fi
sed "s|SCRIPT|${WORK_DIR}/${EXPORT_SCRIPT} |" ${WORK_DIR}/conf/export.conf >> ${WORK_DIR}/conf/export.conf.tmp
crontab -u root ${WORK_DIR}/conf/export.conf.tmp > ${LOG_FILE} 2>&1
result=$?
if [ ${result} != ${SUCC} ];then
echo_log "The format of the configuration file ${CONF_FILE} is not correct."
return ${FAIL}
fi
/etc/init.d/cron restart > ${LOG_FILE} 2>&1
echo_log "The data export task has been registrated in your system successfully."
return ${SUCC}
}
function main()
{
def_global_var
base_check
tag=$?
if [ "${tag}" != "0" ]; then
echo_log "callback base_check function return fail"
exit ${tag}
fi
regist_export
return ${SUCC}
}
main $@
2 楼
ericFang
2010-05-21
upload.sh
#!/bin/bash
function echo_log()
{
local date_time=`date '+%Y%m%d:%H%M%S'`
#echo "$1"
echo "${date_time} $1" >> /opt/export_apn/log/apn_sub.log
return 0
}
file_name=/opt/export_apn/conf/apn.conf
ip=`grep ip ${file_name} |cut -d: -f2`
if [ "${ip}" != "" ];then
ping ${ip} -c 1 -w 1 > /dev/null 2>&1
if [ $? -ne 0 ];then
echo_log " Ftp ip is invalid !"
exit
fi
else
echo_log "Ftp ip is must !"
exit
fi
user=`grep user ${file_name}|cut -d: -f2`
password=`grep password ${file_name}|cut -d: -f2`
directory=`grep directory ${file_name}|cut -d: -f2`
if [ "${directory}" = "" ];then
echo_log "Ftp directory is must !"
exit
fi
upload_file_name=$1
if [ "${upload_file_name}" = "" ];then
upload_file_name=*
fi
re=`ftp -n -v ${ip} << END
user ${user} ${password}
binary
hash
cd ${directory}
lcd /opt/export_apn/output
prompt
mput ${upload_file_name}
bye
END`
is_flag=`echo ${re}|grep "Not logged" |wc -l`
if [ "${is_flag}" = "1" ];then
echo_log "Ftp user or password error!!"
else
is_flag=`echo ${re}|grep "successfully" |wc -l`
if [ "${is_flag}" = "1" ];then
echo_log "Ftp upload file successfully"
else
echo_log "Ftp Connection excption!"
fi
fi
#!/bin/bash
function echo_log()
{
local date_time=`date '+%Y%m%d:%H%M%S'`
#echo "$1"
echo "${date_time} $1" >> /opt/export_apn/log/apn_sub.log
return 0
}
file_name=/opt/export_apn/conf/apn.conf
ip=`grep ip ${file_name} |cut -d: -f2`
if [ "${ip}" != "" ];then
ping ${ip} -c 1 -w 1 > /dev/null 2>&1
if [ $? -ne 0 ];then
echo_log " Ftp ip is invalid !"
exit
fi
else
echo_log "Ftp ip is must !"
exit
fi
user=`grep user ${file_name}|cut -d: -f2`
password=`grep password ${file_name}|cut -d: -f2`
directory=`grep directory ${file_name}|cut -d: -f2`
if [ "${directory}" = "" ];then
echo_log "Ftp directory is must !"
exit
fi
upload_file_name=$1
if [ "${upload_file_name}" = "" ];then
upload_file_name=*
fi
re=`ftp -n -v ${ip} << END
user ${user} ${password}
binary
hash
cd ${directory}
lcd /opt/export_apn/output
prompt
mput ${upload_file_name}
bye
END`
is_flag=`echo ${re}|grep "Not logged" |wc -l`
if [ "${is_flag}" = "1" ];then
echo_log "Ftp user or password error!!"
else
is_flag=`echo ${re}|grep "successfully" |wc -l`
if [ "${is_flag}" = "1" ];then
echo_log "Ftp upload file successfully"
else
echo_log "Ftp Connection excption!"
fi
fi
1 楼
ericFang
2010-05-21
apn_sub.sh
#!/bin/bash
WORK_DIR=/opt/export_apn
LOG_FILE=${WORK_DIR}/log/apn_sub.log
FILE_PATH=${WORK_DIR}/output
RESERVE_NUM=6
function echo_log()
{
local date_time=`date '+%Y%m%d:%H%M%S'`
#echo "$1"
echo "${date_time} $1" >> ${LOG_FILE}
return 0
}
function export_apn_count()
{
local create_time=`date '+%Y-%m%d-%H%M%S'`
file_name=apn_sub_count${create_time}.csv
local apn_sub_count=${FILE_PATH}/${file_name}
res="sqlplus -S dbo/huawei <<! >/dev/null 2>&1
set head off
set trimout on
set trimspool on
set newpage none
set feedback off
spool ${apn_sub_count}
select 'APN,SUBSCRIBER AMOUNT' FROM DUAL;
select s.apn||','||count(distinct sp.SUBSCRIBERID)
from T_SERVICE s ,T_PCC_SUBSCRIPTION sp
where s.apn is not null and s.id=sp.SERVICEID group by s.apn;
spool off
quit
!"
result1=$(su - oracle -c "${res}")
tag=`head -10 ${apn_sub_count}|grep -c 'ORA-'`
if [ "${tag}" != "0" ]; then
echo_log "Export apn data to ${apn_sub_count} fail!! "
fi
return ${tag}
}
#
function export_apn_info()
{
local create_time=`date '+%Y-%m%d-%H%M%S'`
file_name=apn_sub_info${create_time}.csv
local apn_sub_info=${FILE_PATH}/${file_name}
res="sqlplus -S dbo/huawei <<! >/dev/null 2>&1
set head off
set trimout on
set trimspool on
set newpage none
set feedback off
spool ${apn_sub_info}
select 'SUBSCRIBER Id,MSISDN' FROM DUAL;
select distinct P.SUBSCRIBERIDENTIFY,',',P.MSISDN
from T_SERVICE S,T_PCC_SUBSCRIBER P,T_PCC_SUBSCRIPTION SP
where S.APN='$1' AND S.ID=SP.SERVICEID AND SP.SUBSCRIBERID=P.ID;
spool off
quit
!"
result1=$(su - oracle -c "${res}")
tag=`head -10 ${apn_sub_info}|grep -c 'ORA-'`
if [ "${tag}" != "0" ]; then
echo_log "Export apn data to ${apn_sub_info} fail!! "
fi
return ${tag}
}
# 检测数据库
function check_db()
{
tag=" lmtruntime -5 sqlplus -S / as sysdba <<! 2>/dev/null
set head off
select open_mode from v\\\$database;
quit
!"
result=$(su - oracle -c "${tag}")
if [ $(echo "${result}"|grep -c "READ WRITE") = "0" ]; then
return 1
fi
return 0
}
function del_file()
{
local duration=`grep duration ${WORK_DIR}/conf/apn.conf|cut -d: -f2`
local date_time=`date '+%Y%m%d'`
local del_re=`find ${FILE_PATH} -type f -mtime +${duration} -exec rm {} \;`
return 0
}
function main()
{
del_file
if [ "$(whoami)" != "root" ]; then
echo_log "Current user must be 'root'"
exit
fi
check_db
tag=$?
if [ "${tag}" != "0" ]; then
echo_log "The database is invalid"
exit
fi
if [ "$1" != "" ]; then
export_apn_info $1
else
export_apn_count
fi
tag=$?
if [ "${tag}" != "0" ]; then
echo_log "callback export_apn function fail!! "
exit ${tag}
fi
/${WORK_DIR}/upload.sh ${file_name}
return 0
}
main "$@"
#!/bin/bash
WORK_DIR=/opt/export_apn
LOG_FILE=${WORK_DIR}/log/apn_sub.log
FILE_PATH=${WORK_DIR}/output
RESERVE_NUM=6
function echo_log()
{
local date_time=`date '+%Y%m%d:%H%M%S'`
#echo "$1"
echo "${date_time} $1" >> ${LOG_FILE}
return 0
}
function export_apn_count()
{
local create_time=`date '+%Y-%m%d-%H%M%S'`
file_name=apn_sub_count${create_time}.csv
local apn_sub_count=${FILE_PATH}/${file_name}
res="sqlplus -S dbo/huawei <<! >/dev/null 2>&1
set head off
set trimout on
set trimspool on
set newpage none
set feedback off
spool ${apn_sub_count}
select 'APN,SUBSCRIBER AMOUNT' FROM DUAL;
select s.apn||','||count(distinct sp.SUBSCRIBERID)
from T_SERVICE s ,T_PCC_SUBSCRIPTION sp
where s.apn is not null and s.id=sp.SERVICEID group by s.apn;
spool off
quit
!"
result1=$(su - oracle -c "${res}")
tag=`head -10 ${apn_sub_count}|grep -c 'ORA-'`
if [ "${tag}" != "0" ]; then
echo_log "Export apn data to ${apn_sub_count} fail!! "
fi
return ${tag}
}
#
function export_apn_info()
{
local create_time=`date '+%Y-%m%d-%H%M%S'`
file_name=apn_sub_info${create_time}.csv
local apn_sub_info=${FILE_PATH}/${file_name}
res="sqlplus -S dbo/huawei <<! >/dev/null 2>&1
set head off
set trimout on
set trimspool on
set newpage none
set feedback off
spool ${apn_sub_info}
select 'SUBSCRIBER Id,MSISDN' FROM DUAL;
select distinct P.SUBSCRIBERIDENTIFY,',',P.MSISDN
from T_SERVICE S,T_PCC_SUBSCRIBER P,T_PCC_SUBSCRIPTION SP
where S.APN='$1' AND S.ID=SP.SERVICEID AND SP.SUBSCRIBERID=P.ID;
spool off
quit
!"
result1=$(su - oracle -c "${res}")
tag=`head -10 ${apn_sub_info}|grep -c 'ORA-'`
if [ "${tag}" != "0" ]; then
echo_log "Export apn data to ${apn_sub_info} fail!! "
fi
return ${tag}
}
# 检测数据库
function check_db()
{
tag=" lmtruntime -5 sqlplus -S / as sysdba <<! 2>/dev/null
set head off
select open_mode from v\\\$database;
quit
!"
result=$(su - oracle -c "${tag}")
if [ $(echo "${result}"|grep -c "READ WRITE") = "0" ]; then
return 1
fi
return 0
}
function del_file()
{
local duration=`grep duration ${WORK_DIR}/conf/apn.conf|cut -d: -f2`
local date_time=`date '+%Y%m%d'`
local del_re=`find ${FILE_PATH} -type f -mtime +${duration} -exec rm {} \;`
return 0
}
function main()
{
del_file
if [ "$(whoami)" != "root" ]; then
echo_log "Current user must be 'root'"
exit
fi
check_db
tag=$?
if [ "${tag}" != "0" ]; then
echo_log "The database is invalid"
exit
fi
if [ "$1" != "" ]; then
export_apn_info $1
else
export_apn_count
fi
tag=$?
if [ "${tag}" != "0" ]; then
echo_log "callback export_apn function fail!! "
exit ${tag}
fi
/${WORK_DIR}/upload.sh ${file_name}
return 0
}
main "$@"
相关推荐
Linux Shell Scripting Cookbook(3rd) 英文mobi 第3版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书
Proper usage of shell commands can easily solve many complex tasks with a few lines of code, but most linux users don't have the right know-how to use the Linux shell to its full potential. ...
Linux Shell脚本手册 - 第三版,. This book will take you through useful real-world recipes designed to make your daily life easier when working with the shell.
Linuxshell大作业.pdf
LINUX SHELL命令练习 题
LinuxShell脚本编程实例.pdf
本书由浅入深、循序渐进地详细讲解了LinuxShell编程的基本知识。这些基本知识包括Shell编程的基本知识、文本处理的工具和方法、正则表达式、Linux系统知识等。 本书旨在通过理清Linux Shell编程的脉络,从基本概念...
LinuxShell编程基础教程.pdf
linux shell学习资料:Linux Shell从入门到精通、Linux Shell编程艺术、Linux Shell脚本攻略
Learning Linux Shell Scripting 2nd Edition pdf Break through the practice of writing tedious code with shell scripts Key Features Learn to impeccably build shell scripts and develop advanced ...
Linux Shell编程基础 1.调用shell 2.注释 3.执行shell脚本 4.接收用户输入并赋值给变量 5.Bash中的变量 1. 环境变量 1.1HOME 2.2LANG 3.3SHELL 4.4PATH 5.5PS1 6.6PS2 7.7只读环境变量 8.8永久修改环境变量 2. 位置...
The Art of Linux Shell Scripting Shell是unix/linux操作系统与用户进行交互的重要接口,是unix/linux系统中最重要的软件之一。一直以来,shell编程是系统管理员必备的高级技能,通过学习shell编程,可以让计算机...
Linux Shell实例精讲,大量实例shell,容易理解和实践
linux shell编程指南linux shell编程指南linux shell编程指南linux shell编程指南linux shell编程指南linux shell编程指南linux shell编程指南linux shell编程指南linux shell编程指南linux shell编程指南linux ...
Linux Shell常用技巧Linux Shell常用技巧
linux shell 视频教学 轻松学习shell
linux shell 应用.chm,学习linux shell资料
linuxShell脚本编写实验7报告.pdf
Linux shell脚本全面学习 1. Linux 脚本编写基础 1.1 语法基本介绍 1.1.1 开头 程序必须以下面的行开始(必须方在文件的第一行): #!/bin/sh 符号#!用来告诉系统它后面的参数是用来执行该文件的程序。在这个例子中...