一個查找並上傳已更新文件的ftp腳本
#!/bin/ksh
########################################################
#Date: 2012-12-19
#name: swiftftp.sh
#decription: 無限循環,每1分鐘執行一次,查找當前目錄中
修改時間超過1分鐘但未超過
# 2分鐘的文件,ftp到前置上
########################################################
www.2cto.com
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
}
www.2cto.com
#最外層循環
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