# FTP script variable issue

**URL:** <https://community.unix.com/t/ftp-script-variable-issue/308975>\
**Category:** Shell Programming and Scripting\
**Created:** [April 24, 2012, 9:40am UTC](https://community.unix.com/t/ftp-script-variable-issue/308975 "2012-04-24T09:40:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sonu\_pal](https://community.unix.com/user_avatar/community.unix.com/sonu_pal/32/2008_2.png) [@sonu\_pal](https://community.unix.com/u/sonu_pal)\
**Post date:** [April 24, 2012, 9:40am UTC](https://community.unix.com/t/ftp-script-variable-issue/308975/1 "2012-04-24T09:40:15Z")

</div>

Hi ,

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

```plaintext
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](http://podftiapp02.com) and [podftiapp01.com](http://podftiapp01.com)  
respectively.

eg

```plaintext
ftp -n -v UDL_FTIHOST_1

```

Similary it is happening for FTIUSER and FTPWD

Thanks a lot for advice

Regards,  
Sonu

---

<div class="post-metadata">

**Author:** ![methyl](https://community.unix.com/letter_avatar/methyl/32/5_5575768a8748004e209b776fc1b2916d.png) [@methyl](https://community.unix.com/u/methyl)\
**Post date:** [April 24, 2012, 11:14am UTC](https://community.unix.com/t/ftp-script-variable-issue/308975/2 "2012-04-24T11:14:27Z")

</div>

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.

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![sonu\_pal](https://community.unix.com/user_avatar/community.unix.com/sonu_pal/32/2008_2.png) [@sonu\_pal](https://community.unix.com/u/sonu_pal)\
**Post date:** [April 24, 2012, 2:47pm UTC](https://community.unix.com/t/ftp-script-variable-issue/308975/3 "2012-04-24T14:47:51Z")

</div>

Hi ,

I am using the below complete code:

```nohighlight
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

---

<div class="post-metadata">

**Author:** ![methyl](https://community.unix.com/letter_avatar/methyl/32/5_5575768a8748004e209b776fc1b2916d.png) [@methyl](https://community.unix.com/u/methyl)\
**Post date:** [April 24, 2012, 5:05pm UTC](https://community.unix.com/t/ftp-script-variable-issue/308975/4 "2012-04-24T17:05:30Z")

</div>

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".

```nohighlight
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.
