标签:
我们知道ftp 只能用来上传或者下载文件,一次单个或者多个,怎么实现将文件夹的上传和下载呢?
可以利用先在remote ip上建立一个相同的文件夹目录,然后将文件放到各自的目录中去
1、循环遍历出要上传的文件夹中的文件夹目录
2、遍历出每个文件夹下的文件
3、逐一上传文件
ftp 命令

图中有众多的交互,我们不想让他显示这些交互
可以使用参数 屏蔽掉
ftp [-v] [-n] [-i] [-d] [-g] [-s:filename] [-a] [-w:windowsize] [computer]
#!/bin/bash host=‘127.0.0.1‘ user=‘root‘ passwd=‘1988‘ updir=/opt/123 todir=123
dirs=`find $updir -type d -printf $todir/‘%P\n‘| awk ‘{if ($0 == "")next;print "mkdir " $0}‘` files=`find $updir -type f -printf ‘put %p %P \n‘` ftp -nv $host <<EOF user ${user} ${passwd} type binary $dirs cd $todir $files quit EOF
命令解析:
dirs=`find $updir -type d -printf $todir/‘%P\n‘| awk ‘{if ($0 == "")next;print "mkdir " $0}‘` 
find $updir -type d -printf $todir/‘%P\n‘:将上传的目录格式化输出;%P只输出指定目录下的文件夹,而输出他的上层目录,所以要加上一个上层目录
awk 中的{if($0=="")next},next跳过了目录是空的情况
awk next语句使用:在循环逐行匹配,如果遇到next,就会跳过当前行,直接忽略下面语句。而进行下一行匹配。
next的实例:
text.txt 内容是:
a
b
c
d
e
 
[chengmo@centos5 shell]$ awk ‘NR%2==1{next}{print NR,$0;}‘ text.txt     
2 b
4 d
当记录行号除以2余 1,就跳过当前行。下面的print NR,$0也不会执行。 下一行开始,程序有开始判断NR%2 值。这个时候记录行号是:2 ,就会执行下面语句块:‘print NR,$0‘
要求:
文件:text.txt 格式:
web01[192.168.2.100]
httpd            ok
tomcat               ok
sendmail               ok
web02[192.168.2.101]
httpd            ok
postfix               ok
web03[192.168.2.102]
mysqld            ok
httpd               ok
 
需要通过awk将输出格式变成:
web01[192.168.2.100]:   httpd            ok
web01[192.168.2.100]:   tomcat               ok
web01[192.168.2.100]:   sendmail               ok
web02[192.168.2.101]:   httpd            ok
web02[192.168.2.101]:   postfix               ok
web03[192.168.2.102]:   mysqld            ok
web03[192.168.2.102]:   httpd               ok
 
分析:
分析发现需要将包含有“web”行进行跳过,然后需要将内容与下面行合并为一行。
[chengmo@centos5 shell]$ awk ‘/^web/{T=$0;next;}{print T":\t"$0;}‘ test.txt
web01[192.168.2.100]:   httpd            ok
web01[192.168.2.100]:   tomcat               ok
web01[192.168.2.100]:   sendmail               ok
web02[192.168.2.101]:   httpd            ok
web02[192.168.2.101]:   postfix               ok
web03[192.168.2.102]:   mysqld            ok
web03[192.168.2.102]:   httpd               ok
files=`find $updir -type f -printf ‘put %p %P \n‘`
这一命令是找出目录中的文件,然后拼成put 语句
标签:
原文地址:http://www.cnblogs.com/ggbond1988/p/4816044.html