现在的位置: 首页 > 综合 > 正文

Linux/Unix实现自动ftp的shell脚本

2017年08月16日 ⁄ 综合 ⁄ 共 1987字 ⁄ 字号 评论关闭

#!/bin/ksh
. /tbill/app/.profile

sqlplus err/err123@billing @/tbill/data/recyclepath/tmp/ftp_file_data.sql

onInit()
{
  USERHOME=$HOME
  if [ -f "/tbill/data/recyclepath/tmp/autoftp.conf" ];then
    . /tbill/data/recyclepath/tmp/autoftp.conf
    echo machine $host > $USERHOME/.netrc
    echo login $user >> $USERHOME/.netrc
    echo password $passwd >> $USERHOME/.netrc

    chmod 600 $USERHOME/.netrc
    echo =============================
    echo "host=$host"
    echo "user=$user"
    echo "password=x"
    echo "remote dir=$remoteDir"
    echo "source dir=$sourceDir"
    echo "success dir=$backupDir"
    echo "pattern=$pattern "

    echo =============================
    return 0
  else
    return 1
  fi

}

showinfo()
{
  echo NAME
  echo "      autoftp  -automatic upload file to ftp server"
  echo SYNOPSIS
  echo "      autoftp"
}
checkdir()
{
  if [ ! -d $sourceDir ];then
    echo "ERROR!    source dir $sourceDir is not found!"
    return 1
  fi
  if [ ! -d $backupDir ];then
    mkdir $backupDir
  fi
  return 0
}
putFiles()
{
ftp -i $host << EOF
cd $remoteDir
mput $@
bye
EOF
}
#############  main procedure ###################
DIRNAME=`date +%Y%m%d`
if [ $# -eq 1 -a "$1" = "-v" ] ;then
  showinfo
  exit
fi
#======================================================
#backup .netrc file
if [ -f ~/.netrc ]; then
  mv ~/.netrc ~/.netrc.old
fi
#======================================================

onInit
if [ $? -ne 0 ];then
  echo "ERROR!    autoftp.conf not exist! please prepare it"   
  exit 1
fi

checkdir
if [ $? -ne 0 ];then
  exit 1
fi
#begin ftp
date
echo "begin ftp"
cd $sourceDir

while read file;
do
    if [ -f $sourceDir$file ]; then
     touch "$file.chk"
     mv $file "$file.txt"
  putFiles "$file.txt $file.chk"
  mv $file.txt /tbill/data/recyclepath/tmp/$backupDir
  rm $file.chk
  echo "put $file.txt $file.chk done!"
    fi
done <<!
`ls -1|grep "$pattern"`
!

date
echo "ftp end"

#=============================================
if [ -f ~/.netrc ]; then
  rm -f ~/.netrc
fi
if [ -f ~/.netrc.old ]; then
  mv ~/.netrc.old ~/.netrc
fi
#============================================

###*************************************配置文件
host=10.100.5.6
user=noserv
passwd=noserv
sourceDir=/tbill/data/other_path/
backupDir=`date +%Y%m%d`
remoteDir=./
#使用正则表达式描述
pattern="^report.*"

抱歉!评论已关闭.