FTP script variable issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting FTP script variable issue
# 1  
Old 04-24-2012
FTP script variable issue

Hi ,

I have a following code ,I need to send two files on FTP server using single code
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

Code:
ftp -n -v UDL_FTIHOST_1


Similary it is happening for FTIUSER and FTPWD

Thanks a lot for advice

Regards,
Sonu



Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 04-24-2012 at 11:13 AM.. Reason: Please use code tags
# 2  
Old 04-24-2012
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.

Code:
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.

Last edited by methyl; 04-24-2012 at 12:44 PM..
# 3  
Old 04-24-2012
Hi ,

I am using the below complete code:
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
# 4  
Old 04-24-2012
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".

Code:
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.

Last edited by methyl; 04-24-2012 at 06:06 PM.. Reason: Append to $LOGFILE or we overwrite it
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue with the shell script after storing the directory in a variable

FTP is connecting to the server but i am getting an error - Enter if the env is dev or test or prod: test Please enter the id no : xxxxxxx Connected to xxxx 220 (vsFTPd 2.2.2) 331 Please specify the password. 230 Login successful. ?Invalid command ?Invalid command ?Invalid command... (3 Replies)
Discussion started by: chandraprakash
3 Replies

2. Shell Programming and Scripting

FTP command issue in shell script

Hi All I am using following code in my shell script to send the file to a mainframe server. ftp -in $FTP_IP_SEND <<END_OF_FTP >$LOG_DIR/ProviderExportFTP.log quote user $FTP_USER_SEND quote pass $FTP_PASS_SEND ascii send ./ProviderExport.txt 'PROJ.PDRCACTS.FD87050.EXPORT' (REPLACE... (3 Replies)
Discussion started by: Renjith180282
3 Replies

3. Shell Programming and Scripting

variable issue in a bash in a shell script

Hi, I am trying to write a PBS shell script that launches a bash process. The issue is that the bash process needs a variable in it and the shell script is interpreting the variable. How do I pass this as a literal string? Here is my code snippit: TMP=".fasta" FILEOUT=$FILE$TMP cd... (2 Replies)
Discussion started by: bioBob
2 Replies

4. Shell Programming and Scripting

How to pass variable to ftp script?

how can I use shell variable inside ftp script as in below:- the case as below:- I have folder names as (Sunday,Monday...etc) and inside these folders I am trying to get files where their names are "mf_usm_ssnc*_mo0'HOUR''MIN'". where HOUR= hour as in `date "+%H"` - 2 MIN= either... (2 Replies)
Discussion started by: ahmad.diab
2 Replies

5. Shell Programming and Scripting

using a variable within ftp shell script

hi all i have a problem. i have a file called 20091015_extract.csv in my shell script i set the following variable: TODAY=`date +%Y%m%d` when i start an ftp session (within my shell script) I want to transfer the file like this: put $TODAY_extract.csv however, ftp is not... (2 Replies)
Discussion started by: soliberus
2 Replies

6. UNIX for Advanced & Expert Users

FTP-Shell Script-Performance issue

Hello All, Request any one of Unix/Linux masters to clarify on the below. How far it is feasible to open a new ftp connection for transferring each file when there are multiple files to be sent. I have developed shell script to send all files at single stretch but some how it doesnt suit to... (3 Replies)
Discussion started by: RSC1985
3 Replies

7. Shell Programming and Scripting

Performance issue with ftp script.

Hi All, I have written a script to FTP files from local server to remote server. When i try it for few number of files the scripts runs successfully. But the same script when i run for 200-300 files it gives me performanace issue by aborting the connection. Please help me out to improve the... (7 Replies)
Discussion started by: Shiv@jad
7 Replies

8. Shell Programming and Scripting

please help ftp script issue

hi, I have ftp script where the user name has "\" in it and my script unable to detect it....here is what I am doing... `ftp -vin <<- FTP >> log.txt 2>&1 user ibm\user password FTP` my log says.. user ibmuser not found... please help..... (4 Replies)
Discussion started by: mgirinath
4 Replies

9. UNIX for Dummies Questions & Answers

passing ip address as a variable to script for ftp

Still a Beginner here .. Does anyone no how to get the IP address of the machine thats logged in (bearing in mind there will be others logged in) while they are logged in to the Unix server and pass this as a variable to a shell script so as I can FTP files to that machine via a shell script, at... (2 Replies)
Discussion started by: Gerry405
2 Replies

10. UNIX for Advanced & Expert Users

Unix Shell script FTP Issue

Hi All We have a number of FTP Shell scripts running every day on a scheduled basis. The issue we have is if the host connection is not available after a little while ftp times out and the script hangs as the shell returns to the ftp> prompt waiting for input from a user. Any ideas how... (2 Replies)
Discussion started by: mlucas
2 Replies
Login or Register to Ask a Question