Problems uploading file within a do while loop

75 views Asked by At

Hi I have a code that keeps giving me "syntax error: unexpected end of file". Below is the code sample. May I know what is wrong with it? Cos i need to do a comparison and then upload the code.

The first part of the code to download the file has no problem. the second part however just refuse to move

ftp -in << END_DL
open $ftp_site
user $username $password

cd $REMOTE_FOLDER
get $FILE_NAME

close 
bye
END_DL

declare -a folders=( $(cat $LOCAL_FOLDER/files.json | jq '.files' | jq 'keys' | jq '.[]') )
diffFiles=()
for folder in "${folders[@]}"
do
    declare -a files=( $(cat $LOCAL_FOLDER/files.json | jq '.files.'${folder} | jq 'keys' | jq '.[]') )
    for file in "${files[@]}"
    do
        # echo "$(cat $LOCAL_FOLDER/files.json | jq '.files.'${folder}'['${file}']')"
        # Compare with the remote file json
        a=$(cat $LOCAL_FOLDER/files.json | jq '.files.'${folder}'['${file}']')
        b=$(cat $FILE_NAME | jq '.files.'${folder}'['${file}']')

        if [[ "$a" == "$b" && -n $a ]]
        then
            echo "Equal"
        else
            echo "$file in ${folder} folder Not Equal. Transferring file now"
            folder=${folder:(1)}
            folder=${folder%?}
            file=${file:(1)}
            file=${file%?}
            diffFiles+=$folder/$file
            ftp << END_UL
            close
            bye
            END_UL
        fi
    done
done
1

There are 1 answers

0
paxdiablo On BEST ANSWER

Here-docs (as done with <<) are notoriously picky about indentation of the end marker. If it's indented differently to what's specified, it won't be recognised as the end marker, and you'll get an error message exactly like what you're seeing.

The quickest fix is probably to use <<- instead of << - that variant strips off all leading tabs from the here-doc lines1, including that of the end marker, meaning that you can indent it more naturally (otherwise, the end marker would have to be at the start of the line).

In other words, this is wrong (the .___ sequences represent a tab character):

.___.___ftp <<END_UL
.___.___.___close
.___.___.___bye
.___.___END_UL

but either of these will be okay:

.___.___ftp <<END_UL     .___.___ftp <<-END_UL
.___.___.___close        .___.___.___close
.___.___.___bye          .___.___.___bye
END_UL                   .___.___END_UL

You can also use other methods to achieve the same result, such as:

(
    echo close
    echo bye
) | ftp

or:

printf "close\nbye" | ftp

if you don't want to concern yourself with here-docs.


1 Obviously that's not going to work if you want leading tabs in your input stream but, at least in this case, that doesn't appear to be so.