Unix Shell Script循环只能执行一次

时间:2014-04-17 14:35:40

标签: shell unix ftp backup sh

我的shell脚本有问题,应该将备份文件上传到FTP服务器

我创建了一个for循环来执行ftp函数,但不幸的是它只能工作一次。

有人可以帮忙吗?提前谢谢。

#!/bin/sh
# FTP Backup
# Version : 2.0
# Updated : 03/04/2014

#MONITOR MODE
TSCMON=off

echo "-------- FTP BACKUP v2 --------"
echo "#         version 2.0         #"
echo "-------------------------------"


doFTP () {
        echo "Invoking doFTP()"
        echo "doFTP()->IP=$1->USR->$2->PWD->$3"

        declare -a sFILESET=("${!7}")
        #echo "DEBUGGGGGGGGG -------------------> ${sFILESET[@]}"

        echo "#!/bin/sh" > client.ftp
        echo " FILECOUNT= $6"
        echo "/usr/bin/ftp -inv $1 << END_SCRIPT" >> client.ftp
        echo "quote USER $2" >> client.ftp
        echo "quote PASS $3" >> client.ftp
        echo "lcd $4" >> client.ftp
        echo "mkdir $5" >> client.ftp
        echo "cd $5" >> client.ftp

        for ((i=0;i<$6;i++)); do
                #echo "DEBUG i=$i"
                echo " UPLOADING FILE -->  ${sFILESET[$i]}"
                echo "mput ${sFILESET[$i]}" >> client.ftp
        done

        echo "bye" >> client.ftp
        echo "END_SCRIPT" >> client.ftp
        chmod 755 client.ftp
        ./client.ftp
}


#Export Environment
HOST=$(hostname)
IP=$(/sbin/ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}')
DBSID=$ORACLE_SID
LOCALPATH='/orabackup'
DESTPATH="${HOST}_${IP}/${DBSID}/export"
echo "--> Exporting Environment ..."


#DATE CONFIGURATION
dtoday=$(date +"%Y%m%d")
dyesterday=$(date --date="-1 day" +"%Y%m%d")
echo "--> Declaring DATE CONFIGURATION ..."
echo " TODAY = ${dtoday}"
echo " YESTERDAY = ${dyesterday}"


#FILE CONFIGURATION
declare -a FILESET
echo "--> Declaring FILE CONFIGURATION ..."
# Added by iPattZ 10Apr2014 : Supported Wildcard *#
FILESET[${#FILESET[@]}]="bfile_${dyesterday}.log.gz"
#FILESET[${#FILESET[@]}]="bfile_${dyesterday}.dmp.gz"
echo " FILESET.SIZE-> ${#FILESET[*]}"
echo " FILESET.VAL-> ${FILESET[*]}"


#FTP CONFIGURATION
declare -a FTPHOST
echo "--> Declaring FTP CONFIGURATION ..."
FTPHOST[${#FTPHOST[@]}]='192.168.1.199;myuser;mypass
FTPHOST[${#FTPHOST[@]}]='192.168.1.200;myuser;mypass
FTPCNT=${#FTPHOST[@]}
echo " Found FTP Server : ${FTPCNT}"
#echo "DEBUG : FTP String : ${FTPHOST[@]}"

for ((i=0;i<FTPCNT;i++)); do

        echo "--> FTP No.$(($i+1)) : Connecting ..."

        IFS=';' read -a j <<< "${FTPHOST[i]}"
        sFTPIP=${j[0]}
        sFTPUSR=${j[1]}
        sFTPPWD=${j[2]}
        sFILECNT=${#FILESET[*]}
        echo " FTPIP->${sFTPIP}"
        echo " FTPUSR=${sFTPUSR}"
        #echo "DEBUG sFILECNT=${sFILECNT}"
        #echo "DEBUG FTPPWD=${sFTPPWD}"

        doFTP $sFTPIP $sFTPUSR $sFTPPWD $LOCALPATH $DESTPATH $sFILECNT "FILESET[@]"
done

1 个答案:

答案 0 :(得分:2)

循环索引i用于doFTP和主脚本。第一次调用doFTP时,i最终会设置为该调用的$6,这很可能大于FTPCNT的值,从而导致退出条件以便在下次检查时满足外部循环。只需使用另一个变量作为每个循环的索引。