Simple sftp script not working - Please help

I have the below sftp script to transfer a file from a linux host(source) to another linux host(target). Public key is already set up in target host and I am able to transfer file using sftp from source to target.
But not sure why the below script(ftp_script) is not working.
Any help in this regard will be highly appreciated.

#!/bin/ksh
sftp user2@target <<!
put test.txt
quit
!

Below is the output from console after running the script

[user1@source ~]$./ftp_script
Remote system type is POSIX\
sftp>
[user1@source ~]$

You'd probably be better off using the batch command functionality included with sftp.

Create a file with the commands.

put test.txt
bye

Then run sftp like so:

sftp -b <file with commands> user2@target

Hope this helps.

Since both sftp and scp are enabled by ssh access, you might try scp with its simpler syntax and batch friendliness.

Thank you.. batch command functionality worked.

sftp -B <file with commands> user2@target