954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Automating an FTP upload

Hello,

I have stared at this script for the last two days and am unable to find the cause of my error message. Any help would be greatly appreciated.

The script is

#!/bin/sh
for i in '/xxx/*'
do
        if [ -f /xxx/$i ]
        then
                HOST='xxx.xxx.com'
                USER='xxxx'
                PASSWD='xxxx'
                FILE='$i'
                ftp -n $HOST <<END_SCRIPT
                quote USER $USER
                quote PASS $PASSWD
                ascii
                put $FILE
                END_SCRIPT
        fi
done


I have removed some of the site specifics but this is the basic script. If I remove the For loop and If condition and just specify the file name, the ftp works. Introduction of the loops causes:

uploader.sh: 19: Syntax error: end of file unexpected (expecting "fi")


each time. The error is always listed as one line past the length of the script. I am completely baffled as to what I am missing in this script.

medaugh
Newbie Poster
3 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 

I coould be completely wrong, because I always put it against the left edge anyway, but I believe the "END_SCRIPT" marker you are using to end the so-called here document, needs to be against the left hand side, in otherwords at the beginning of the new line.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

That worked great. Thanks for saving me from a very long weekend.

:p

medaugh
Newbie Poster
3 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 

Hello, I have stared at this script for the last two days and am unable to find the cause of my error message. Any help would be greatly appreciated. The script is

#!/bin/sh
for i in '/xxx/*'
do
        if [ -f /xxx/$i ]
        then
                HOST='xxx.xxx.com'
                USER='xxxx'
                PASSWD='xxxx'
                FILE='$i'
                ftp -n $HOST <<END_SCRIPT
                quote USER $USER
                quote PASS $PASSWD
                ascii
                put $FILE
                END_SCRIPT
        fi
done

I have removed some of the site specifics but this is the basic script. If I remove the For loop and If condition and just specify the file name, the ftp works. Introduction of the loops causes:

uploader.sh: 19: Syntax error: end of file unexpected (expecting "fi")
each time. The error is always listed as one line past the length of the script. I am completely baffled as to what I am missing in this script.

I wouldn't waste your time trying to shell this in this mannor. Use "expect" - once you have expect loaded which does come with most distros - just run autoexpect, and it will record your network activity of ftping. When your finished, just open the script and edit accordingly.

Hope this helps - here is expect: http://sourceforge.net/projects/expect

zerobreach.com
Newbie Poster
2 posts since Aug 2006
Reputation Points: 10
Solved Threads: 1
 

Greetings,

The method you are using is going to hammer the ftp with a connect and disconnect for every file in that dir. I have re-wrote it to make it more connection friendly, I would consider using the for loop on the send only. Like so:

#!/bin/sh

# Declare Variables
HOST='xxx.xxx.com'
USER='xxxx'
PASSWD='xxxx'

# Start FTP Connection
ftp -n $HOST <

toztech
Newbie Poster
3 posts since Sep 2006
Reputation Points: 10
Solved Threads: 1
 

If you want to indent you can use the indent option for inline files:

if [[ something ]] ; then
    ftp -n $HOST <<-END_SCRIPT
        ...
        ftp commands
        ...
    END_SCRIPT
fi


Note the dash before the introduction to the inline file.

sut
Light Poster
30 posts since Sep 2006
Reputation Points: 20
Solved Threads: 1
 

hi
can anybody help me in automating ftp. following is my program

#!/bin/sh
HOST="192.168.1.227"
USERNAME="ganesh"
PASS="ganesh123"
ftp -n -v $HOST << EOF
USER $USERNAME
PASSWORD $PASS
bin
hash
prompt
get messages.properties
bye
EOF


i have problem in login the FTP

ganeshbonde
Newbie Poster
1 post since Apr 2007
Reputation Points: 10
Solved Threads: 1
 

Start your own thread.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

I'm running on a Linux machine and can't get FTP uploads working. Here's my script:
HOST='ftp.name.com'
USER='username'
PASSWD='mypwd'
FILE='f1020811'
DIR='/data_dir/send'
echo **************************************************************
echo * Attempting FTP *
echo **************************************************************
ftp -n -u -d ftp.name.com <

musicalsailor
Newbie Poster
5 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

Thanks Masijade
This has solved my problem
I had the END_SCRIPT indented with one tab, and removing that has got it going.
MANY THANKS
Derek

Derek001
Newbie Poster
1 post since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

Expect is definitely the way to go... OR you could generate a list of files first (store them in a variable or temporary file), and use ncftpput to do the work, no expect or << required!

Gromit
Posting Whiz in Training
212 posts since Sep 2008
Reputation Points: 47
Solved Threads: 31
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You