"<< Unmatched" in ksh script using sftp

Getting "syntax error at line 19: `<<' unmatched" trying to run sftp in a ksh script.

...snip...
13 for each in $HOSTS;
14 do
15 if [ ! -d /usr/restore/$each ];
16 then mkdir /usr/restore/$each
17 fi
18 cd /usr/restore/$each
19 sftp -b - server-1 <<EOF
20 get /root/restore/$each.tar
21 bye
22 EOF
23 if [ $? -ne 0 ];
24 then echo "SFTP of $each vitalfiles output to bkupva01 failed." >> 25 failed_hosts.$DATE
26 else continue
27 fi
28 done
...snip...

I've tried sftp -b /dev/stdin server-1 <<EOF also, same result, unmatched <<.

Anyone have any ideas? I'm sure it's simple, but I can't see it to save my life.

Thanks!
mk

Check for any space between '<<' and EOF at line 19

and whether 'EOF' starts exactly at the beginning of the line

1 Like

Post the entire script or at least up to the EOF line.

matrixmadhan you found it! It wasn't the spaces around the <<, but the space in front of the the second EOF.
It was tabbed over to emphasize the loop. Took out the white space and it ran like a champ.

Good eyes, thanks for your help!
mk

I've had the same problem with the Automated SFTP script

Its crazy ...

Thanks to all of ya

better we should start working with scripts in an IDE, which optimizes, aligns, auto corrects the editing :wink:

Hi All,

The final SFTP program can look like this;

sftp -b - server-1 <<-EOF
get /root/restore/$each.tar
bye
EOF

Describing the above program with the (space)/(no-space)/(no-characters) characters to make it more clear to understand the code; (space) means either a whitespace / a tab.

sftp -b - server-1(space)<<-EOF(no-space)(no-characters)
(space)get /root/restore/$each.tar(space)
(space)bye(space)
(no-space)(no-characters)EOF(no-space)(no-characters)
-------------------------------------------

In addition, here is some sample programs for you to test; Hope with this you may undestand the usage of "heredocument". It's better always use a '-' after << and before the first CONTENT tag

Program 1

RESULT="`sqlplus $USER/$PWD@$TNS <<-CONTENT
select sysdate from dual;
exit;
CONTENT
`";
echo "$RESULT"

Never add any other characters to the second tag (Eg:- Never do this [ CONTENT`";]. You will get error.

Describing the above program with the (space)/(no-space) characters;

RESULT="`sqlplus [EMAIL="$USER/$PWD@$TNS\(space\)&lt;&lt;-END\(no-space"]$USER/$PWD@$TNS(space)<<-CONTENT(no-space)[/EMAIL]
(space)select sysdate from dual;(space)
(space)exit;(space)
(no-characters)(no-space)CONTENT(no-space)(no-characters)
(space)`";(space)
echo "$RESULT"


Program 2

cat <<-CONTENT
echo "Hello Foo"
echo "Hello"; Hello "Foo";
CONTENT
CONTENT"
CONTENT

Describing the same program with the (space)/(no-space) characters;

cat(space)<<-CONTENT(no-space)
(no-space)echo "Hello Foo"(no-space)
(no-space)echo "Hello"; Hello "Foo";(no-space)
(space)(space)CONTENT(no-space)
(no-space)CONTENT"(no-space)
(no-space)CONTENT(no-space)

Only the Final CONTENT will match with the First CONTENT tag. Others have either a whitespace / a character attached to it and the KSH will not be able to recognise it, because it considers the whole as a string which doesn't match with the string CONTENT.

Output of the above program
echo "Hello"; Hello "Foo";
CONTENT
CONTENT"

To Conclude the CONTENT tag should not be attached with any characters including white space. Always use the '-' afterthe << and before the first CONTENT tag
Eg:-

cat <<-CONTENT
resolved
CONTENT

Description

cat(space)<<-CONTENT(no-space)(no-characters)
(space)(space)(space)(space)resolved(space)(space)(space)(any-characters)
(no-characters)(no-space)CONTENT(no-space)(no-characters)

Result follows;

resolved

:slight_smile: