0

I need to do an FTP of the files available in the file files_to_download. I have put an FTP script in between but it throws and error saying " Syntax error: end of file unexpected (expecting "done")". Do I need to do an FTP login for downloading the file every time. I want to download all the files in a single FTP login?

  if  [ $update -eq 1 ]
    then
        #echo "File needs to be updated"
        while read file_data
        do
            #echo $file_data
        file_name=`echo $file_data | cut -d':' -f1`  #truncate the file path
        echo $file_name

            #ftp -inv <<! 
            #open ${SERVER}
            #user ${USERNAME} ${PASSWORD}
            #binary
            #cd $REMOTEDIR
            #get server_version
            #lcd $LOCALDIR
            #close
            #quit
            #!    

        done < files_to_download   
fi

2 Answers 2

1

You can use an outline script like this:

{
cat << EOF
open ${SERVER}
user ${USERNAME} ${PASSWORD}
binary
cd ${REMOTEDIR}
get server_version
EOF

sed -e 's/:.*//' -e 's/^/get /' files_to_download

cat <<EOF
lcd ${LOCALDIR}
close
quit
EOF    
} | ftp -inv

The first cat sets up the connection. The sed edits the list of file names into get statements. The final cat puts out the remaining commands. The surrounding { and } send all the output of the commands within to the ftp command. The chances are that simply omitting all the second cat would work fine; the FTP command would read EOF on its input after the final file transfer and then exit of its own accord.

The get server_version can be deleted if server_version was meant to be a file name rather than a request for the version of the FTP server. The lcd probably isn't necessary either.

I've used the ${VAR} notation consistently; the original code used that an $VAR. Consistency is good.

Sign up to request clarification or add additional context in comments.

Comments

0

You should not indent !, that is, you should place it at the beginning of the line, without any whitespaces before it. You indented it, so it's not parsed as the end of the heredoc.

To download all files in a single login, you can print the FTP commands in a subshell. Or, you can also generate read the filenames beforehand and store it into a variable.

4 Comments

Yeah I got it @user4098326...I did a proper indentation and there are no errors now . Is there any way to do a singlt FTP login instead of doing an FTP login in a loop?
In the above script , I need to substitute the value of $file_name in the line "get file_name" in the FTP script . Is that possible?
@SowmyaManmathan: Yes. Have the script generate the preamble that logs in, the list of get commands, and the close/quit sequence, and feed that to the ftp command. One login...quite sufficient. And yes, you can have the file name expanded in the here document.
@JonathanLeffler . I am not exactly getting it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.