Vim配合Shell自动上传ftp
shell代码:
#!/bin/bash
#网站配置
a1=('本地目录;主机;yonghuming;mima;远程目录' '本地目录;主机;user;pwd;远程目录')
#选取的网站配置
web=""
#本地根目录
webroot="本地apache网站根目录"
website1=`echo $1|awk '{split($0,tA,/本地apache网站根目录/);print tA[2];}'`
website=`echo $website1|awk 'BEGIN{FS="/"}{print $1}'`
if [ "$website1" = "$website" ];then
website=""
fi
#获得网站配置
for row in ${a1[@]}
do
arr=`echo $row|awk 'BEGIN{FS=";"}{print $1}'`
if [ "$website" = "$arr" ];then
web=$row
fi
done
#获得主机
hostname=`echo $web|awk 'BEGIN{FS=";"}{print $2}'`
echo $web
#获得用户
user=`echo $web|awk 'BEGIN{FS=";"}{print $3}'`
#获得密码
pwd=`echo $web|awk 'BEGIN{FS=";"}{print $4}'`
#获得本地目录
lcd="$webroot$website"
#获得文件路径
filename=$1
#获得文件名
ext=`echo $filename|awk '{st=match($0,/\/[^\/]*$/);tA=substr($0,st+1);printf tA;}'`
#获得远程目录
dir=`echo "$filename;$lcd"|awk 'BEGIN{FS=";"}{len=length($2);st=match($1,/\/[^\/]*$/);tA=substr($1,len,st-len);print tA;}'`
if [ "$dir" = "" ];then
dir="./"
fi
cd=`echo $web|awk 'BEGIN{FS=";"}{print $5}'`"/$dir"
#ftp连接
ftp -n $hostname<<EOF
user $user $pwd
binary
cd "$cd"
put "$filename" "$ext"
ls
bye
EOF
.vimrc添加:
function! Ftp()
let file=expand("%:p")
let cmd='source ~/autoftp.sh "'.file.'"'
let log=system(cmd)
echo log
endfunction
map <F5> :call Ftp()<CR>
map! <F5> <ESC>:call Ftp()<CR>