#!/bin/bash
#author: zhourunfei
#用户名
user=test-hun
#密码
password=test-hun
#sftp的地址
ip=192.168.2.130
#需要上传到的sftp目的目录
destdir=/upload
#本地需要上传文件的目录
sourcedir=/opt/
#需要上传的文件名
filename=act_check.sh
#上传文件
lftp -u $user,$password sftp://$ip <<EOF
cd $destdir
lcd $sourcedir
put $filename
close
quit
EOF
#上传完毕后,检查是否sftp上传成功,检查sftp是否有相应的文件
#将ftp连接后执行命令的标准输出内容输出到7,把7保存在result.txt中
exec 7>&1 1>/opt/result.txt
lftp -u $user,$password sftp://$ip <<EOF
cd $destdir
lcd $sourcedir
ls
close
quit
EOF
##恢复标准输出,以为上面语句将标准输出绑定到fd 7了
exec 1>&7
#关闭7号输出,所以这样就把操作sftp的记录的标准输出都输出到了result.txt中了
exec 7>&-
exit_file=`cat result.txt | grep act_check.sh`
if [ ! -n "$exit_file" ];then
echo "sftp中上传的文件不存在,上传失败!!!"
else
echo "sftp中有上传的文件,上传成功"
fi
参考:https://www.jianshu.com/p/fe23b23839e8
批量上传下载:https://blog.youkuaiyun.com/qq_33195791/article/details/89554235