- 论坛徽章:
- 0
|
确认ftp成功上传文件
这段代码用于ftp上传文件,并确认是否成功。欢迎大家提出意见。
除了 "#/bin/sh", 其余#开头的行都可以移除。你也许不能直接拷贝这些代码,最好用vi将那些不可见的字符删掉。
作者:Wise Huang
公司:American president Lines (China)
email: hnsnet@online.sh.cn.
#Here is the shell script how to ftp send 3 files.
#I like to send some files to remote file server in one batch and must make sure if this batch was ftp sucessful.
#These files looks like following: 2008-01-01 08:00:01.000, ctn 2008-01-01 08:00:01.txt, all 2008-01-01 08:00:01.txt
#Fistly, we must find the file *.000 in local server, if no this file, don't send this batch
#We suppose the local folder is /home/ers/out
#And the remote folder is /
#/bin/sh
ftpusername='guest1'
ftppassword='password123'
#backup the files which will sent
cp /home/samba/ers/out/*.* /home/samba/ers/bak
#In order to avoide the conflict, move the files to a temp folder named "buffer" firstly
mv /home/samba/ers/out/*.* /home/samba/ers/buffer
cd /home/samba/ers/buffer
#get the file *.000, it is a flg file. e.g 2008-01-01 08:00:01.000
ls -rt *.000 > /home/samba/ers/localfile.lst
sed -e "s/.000//g" /home/samba/ers/localfile.lst > /home/samba/ers/localfile.txt
localfile=`head -1 /home/samba/ers/localfile.txt`
ctrfile="ctr "$localfile".txt"
allfile="all "$localfile".txt"
markfile=$localfile".000"
#end of get file name which will be sent.
#put three files in one batch
ftpresults=`ftp -vn<<!
open 220.1.1.2
user $ftpusername $ftppassword
binary
prompt off
cd /buffer
lcd /home/samba/ers/buffer
put "$ctrfile"
put "$allfile"
put "$markfile"
lcd /home/samba/ers
!
`
#Analysis the ftp results, output the ftp results to a log file firstly.
echo $ftpresults > /home/ftp.log
#As the ftp result have no return, so split this log to some lines. use "tr", it is very impotant.
#Then get the key words "bytes sent in"
ftp_success=`echo $ftpresults|tr "." "\n"|grep "bytes sent in"|wc -l`
# Above sentance mean add a return if met ".", and find "bytes sent in", then count the words "bytes sent in"
echo $ftp_success
#If we got three times "bytes sent in", it means ftp three files sucessful.
if [ $ftp_success -eq 3 ] ; then
ftp -n<<!
open 220.1.1.2
user $ftpusername $ftppassword
binary
prompt on
rename /buffer/"$allfile" /"$allfile"
rename /buffer/"$ctrfile" /"$ctrfile"
rename /buffer/"$markfile" /"$markfile"
!
#Rename is a ftp command what move remote files
rm -f /home/samba/ers/buffer/"$ctrfile"
rm -f /home/samba/ers/buffer/"$allfile"
rm -f /home/samba/ers/buffer/"$markfile"
rm -f /home/samba/ers/filelist.txt
fi
unset remotefile
unset ctrfile
unset allfile
unset markfile
exit 0 |
|