免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3755 | 回复: 11
打印 上一主题 下一主题

确认ftp成功上传文件 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-01-21 16:45 |只看该作者 |倒序浏览
确认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

论坛徽章:
0
2 [报告]
发表于 2008-01-21 16:52 |只看该作者
直接执行ftp后用$?来判断不行吗?

论坛徽章:
0
3 [报告]
发表于 2008-01-21 16:55 |只看该作者
没试过

论坛徽章:
0
4 [报告]
发表于 2008-01-21 17:00 |只看该作者
$?就是用来判断命令是否执行成功的特殊标志.

论坛徽章:
0
5 [报告]
发表于 2008-01-21 17:16 |只看该作者
不行的。$?得不到什么结果。也许你可以给个例子

论坛徽章:
0
6 [报告]
发表于 2008-01-21 17:23 |只看该作者
ftp 命令      #execute ftp command
if [ $? -ne 0 ];then       #if ftp command does not execute successfully then print the error message
    echo "error message!"
    exit 1
fi
other command   #if ftp command execute successfully then do other thing next

[ 本帖最后由 hfvbc 于 2008-1-21 17:25 编辑 ]

论坛徽章:
23
15-16赛季CBA联赛之吉林
日期:2017-12-21 16:39:27白羊座
日期:2014-10-27 11:14:37申猴
日期:2014-10-23 08:36:23金牛座
日期:2014-09-30 08:26:49午马
日期:2014-09-29 09:40:16射手座
日期:2014-11-25 08:56:112015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:0315-16赛季CBA联赛之山东
日期:2017-12-21 16:39:1915-16赛季CBA联赛之广东
日期:2016-01-19 13:33:372015亚冠之山东鲁能
日期:2015-10-13 09:39:062015亚冠之西悉尼流浪者
日期:2015-09-21 08:27:57
7 [报告]
发表于 2008-01-21 17:29 |只看该作者
原帖由 hfvbc 于 2008-1-21 17:23 发表
ftp 命令      #execute ftp command
if [ $? -ne 0 ];then       #if ftp command does not execute successfully then print the error message
    echo "error message!"
    exit 1
fi
other command ...


不可能的。

$?只是返回ftp -n这个命令的返回值。
即使第一句open 220.1.1.2命令就出错了,$?仍然是0,因为ftp -n这个命令是执行成功了的。

论坛徽章:
0
8 [报告]
发表于 2008-01-21 17:34 |只看该作者
可能是remote server有问题,或者其他原因,不管ftp成功与否,$?永远是0
谢谢你的代码,我会在研究一下。

论坛徽章:
0
9 [报告]
发表于 2008-01-21 17:36 |只看该作者
那不也可以用$?来判断open 的吗?

论坛徽章:
23
15-16赛季CBA联赛之吉林
日期:2017-12-21 16:39:27白羊座
日期:2014-10-27 11:14:37申猴
日期:2014-10-23 08:36:23金牛座
日期:2014-09-30 08:26:49午马
日期:2014-09-29 09:40:16射手座
日期:2014-11-25 08:56:112015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:0315-16赛季CBA联赛之山东
日期:2017-12-21 16:39:1915-16赛季CBA联赛之广东
日期:2016-01-19 13:33:372015亚冠之山东鲁能
日期:2015-10-13 09:39:062015亚冠之西悉尼流浪者
日期:2015-09-21 08:27:57
10 [报告]
发表于 2008-01-21 17:38 |只看该作者
原帖由 hfvbc 于 2008-1-21 17:36 发表
那不也可以用$?来判断open 的吗?


open是ftp命令的子命令,是Here Doc的内容,是没有办法用$?来判断的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP