FTP script variable issue

Hi ,

I have a following code ,I need to send two files on FTP server using single code

UDL_FTIHOST_1=podftiapp02.com
UDL_FTIUSER_1=dbsunny
UDL_FTIPWD_1=sunny@11
UDL_FTIHOST_2=podftiapp01.com
UDL_FTIUSER_2=dbamy
UDL_FTIPWD_2=amy@12

count=2

i=1
while [ $i -le $count ]
do
 
FTIHOST=$(echo UDL_FTIHOST_${i})
echo $FTIHOST
FTIUSER=$(echo UDL_FTIUSER_${i})
FTIPWD=$(echo UDL_FTIPWD_${i})


ftp -n -v ${FTIHOST} <<EOF > ${LOGFILE}
cd ..
user ${FTIUSER} ${FTIPWD}

i=`expr $i + 1`
echo "--------------------------------------------------------------"

done >> ${LOG_FILE}

However while executing the above code , FTIHOST assigns the value UDL_FTIHOST_1 and UDL_FTIHOST_2 not podftiapp02.com and podftiapp01.com
respectively.

eg

ftp -n -v UDL_FTIHOST_1

Similary it is happening for FTIUSER and FTPWD

Thanks a lot for advice

Regards,
Sonu

Though we would never write script in this manner, the method will only work if you use the Shell eval command to defer processing of the variable until after the value of $i is known. The \$ is important to stop Shell evaluation of the variable name on the first pass.

eval FTIHOST=\${UDL_FTIHOST_$i}
eval FTIUSER=\${UDL_FTIUSER_$i}
eval FTIPWD=\${UDL_FTIPWD_$i}

The eval command has security issues. There is always a better way. In your case a Shell Array might be better but personally I'd have a Shell Function to do the ftp and call it with three parameters.

Ps. The EOF line is missing from the end of the Here Document.
Neither $LOGFILE or $LOG_FILE are set.

Hi ,

I am using the below complete code:

INPUTFILE_1=ADDR3.ALL
INPUTFILE_2="PCPSWAB1.UNDELV.DWNL.TRILL(+1)"
FTP_FILE_1=NVS.DWG.P.GBRSWUDL.ADDR3
FTP_FILE_2=PCPSWAB1.UNDELV.DWNL.TRILL-900-TEXT.u
ftp_mode="ascii"
target_dir_1=".."
target_dir_2="trillium"
count=2
LOGFILE=/srv/udl/logs/udl_ftp2fti.log

UDL_FTIHOST_1=podftiapp02.com
UDL_FTIUSER_1=dbsunny
UDL_FTIPWD_1=sunny@11
UDL_FTIHOST_2=podftiapp01.com
UDL_FTIUSER_2=dbamy
UDL_FTIPWD_2=amy@12

count=2

i=1
while [ $i -le $count ]
do
 

FTIHOST=$(echo UDL_FTIHOST_${i})
FTIUSER=$(echo UDL_FTIUSER_${i})
FTIPWD=$(echo UDL_FTIPWD_${i})
target_dir=$(echo target_dir_${i})
FTP_FILE=$(echo FTP_FILE_${i})
INPUTFILE=$(echo INPUTFILE_${i})


echo "Connecting to FTP server to send UDL file...."
ftp -n -v ${FTIHOST} <<EOF > ${LOGFILE}

lcd ${FILE_DIR}
cd ${target_dir}
user ${FTIUSER} ${FTIPWD}
type ${ftp_mode}
put ${INPUT_FILE} ${FTP_FILE}
EOF

i=`expr $i + 1`
echo "--------------------------------------------------------------"
done >> ${LOGFILE}

Can you let me know how can we implement it thorugh array or using shell function..

thanks for your advice.

sonu

Here we go. Totally untested and unwarrented. Rearranged your script to logical order and inserted a function to do the ftp. Removed all lines which become unneeded. Re-ordered your ftp lines because you must log in before issuing commands and also appended a "bye".

LOGFILE=/srv/udl/logs/udl_ftp2fti.log

# Parameters for first transfer
UDL_FTIHOST_1=podftiapp02.com
UDL_FTIUSER_1=dbsunny
UDL_FTIPWD_1=sunny@11
INPUTFILE_1=ADDR3.ALL
target_dir_1=".."
FTP_FILE_1=NVS.DWG.P.GBRSWUDL.ADDR3

# Parameters for second transfer
UDL_FTIHOST_2=podftiapp01.com
UDL_FTIUSER_2=dbamy
UDL_FTIPWD_2=amy@12
INPUTFILE_2="PCPSWAB1.UNDELV.DWNL.TRILL(+1)"
target_dir_2="trillium"
FTP_FILE_2=PCPSWAB1.UNDELV.DWNL.TRILL-900-TEXT.u


transferfile ()
{
echo "Connecting to FTP server to send UDL file...."
ftp -n -v $1 <<EOF >> ${LOGFILE}
user $2 $3
ascii
lcd $4
cd $6
put $5 $7
bye
EOF
}
########################
# Processing starts here
########################
# First transfer
transferfile "${UDL_FTIHOST_1}" "${UDL_FTIUSER_1}" "${UDL_FTIPWD_1}" \
             "${UDL_FTIPWD_1}" "${INPUTFILE_1}" \
             "${target_dir_1}" "${FTP_FILE_1}" 
# Second transfer
transferfile "${UDL_FTIHOST_2}" "${UDL_FTIUSER_2}" "${UDL_FTIPWD_2}" \
             "${UDL_FTIPWD_2}" "${INPUTFILE_2}" \
             "${target_dir_2}" "${FTP_FILE_2}"

There is still a lot which could go wrong with this script. Most versions of ftp do not like funny filenames or funny characters in passwords.
Good luck.