#!/bin/ksh
########################################################
#Date: 2012-12-19
#name: swiftftp.sh
#decription: 无限循环,每1分钟执行一次,查找当前目录中修改时间超过1分钟但未超过
# 2分钟的文件,ftp到前置上
########################################################
datestr=`date "+%Y%m%d"`
_logname=dynamicftp${datestr}.log
chmod +x *
if [[ ! -f ${_logname} ]];then
touch ${_logname};
fi
#该函数打印参数1的时间戳
# perl usage: perl -e "$a=1+4;exit($a);"
cal_timestamp()
{
perl -e '
use Time::Local;
# get current time
# 参数表示,要求返还minu_num个分钟之前的时间戳yyyymmddhhmm.ss
$minu_num="$ARGV[0]";
localtime(time);
$time1= time -60*$minu_num;
($sec,
$min,
$hr,
$mday,
$mon,
$yr,
$wday,
$yday,
$isdst)=localtime($time1);
$yr=$yr+1900;
$mon=$mon+1;
$beforetime=sprintf("%d%02d%02d%02d%02d.%02d",$yr,$mon,$mday,$hr,$min,$sec);
print "$beforetime"
' "$1" #把函数的第一个参数,用这种形式传递给perl
}
#最外层循环
while [[ 1 -eq 1 ]] ; do
# example use of cal_timestamp:
oneminubef=$( cal_timestamp "1")
echo "1 minutes before:$oneminubef" >> ${_logname}
twominubef=$( cal_timestamp "2")
echo "2 minutes before:$twominubef" >> ${_logname}
# create two older files
touch -t $oneminubef oneminubef.tmp
touch -t $twominubef twominubef.tmp
#find the file list which is younger than 2 minutes but older than 1 minutes
#find usage: find -newer file1 ! file2
touch tempfile01
chmod 777 tempfile01
find . -newer twominubef.tmp ! -newer oneminubef.tmp | grep -v '^./oneminubef.tmp' |grep -v "^./${_logname}" |grep -v "{$0}" > tempfile01
#read the file and strcat the files
a=0
filenames=
while read line
do
line=`echo $line | sed -e 's/^ *//' -e 's/ *$//'` #去除空格
length=`echo ${line} | awk '{print length($1);}'` #计算字符数
if [[ ${length} -gt 0 ]]; then #防止空行
newline=${line#*/} #去除文件名前面的./
filenames=$filenames" "$newline
a=`expr $a + 1`
fi
done < tempfile01
rm -rf tempfile01 oneminubef.tmp twominubef.tmp
echo "files :$filenames" >> ${_logname}
_username=test
_passwd=test
if [[ $a -gt 0 ]]; then
ftp -in 192.168.93.77 << !
user ${_username} ${_passwd}
bi
mput $filenames
bye
!
fi
if [[ $? -eq 0 && a -gt 0 ]]; then
echo `date`" successfully put $a files on server" >> ${_logname}
elif [[ a -gt 0 ]]; then
echo `date`" put files error!" >> ${_logname}
else
echo `date`" no files need to be put!" >> ${_logname}
fi
sleep 60
done