SFTP Does directory exist?

Hi,

Im trying to add some validation into my shell script code that basically checks whether a directory exists before SFTP'ing a file to it. If the directory exists then it will add the file, if not then it should return some kind of message. This is the code I have written so far but with no success:

sftp ${SFTP_ACCOUNT}@${SFTP_ADDR} >${ERROUT} 2>&1 <<EOF
#echo $SFTP_DESTINATION
if [-d $SFTP_DESTINATION && echo "directory exists"];
then
   put ${SOURCE_FILE_NAME} ${SFTP_DESTINATION}
else
  echo "Remote Directory ${SFTP_DESTINATION} does not exist"
fi
bye
EOF

Any ideas on how i do this would be greatly appreciated!

Thanks,
Jack

I don't think we can do that within sftp. Only basic commands like ls works.
But since you have sftp set up, you can make use of ssh to check the directory existence and then proceed with sftp

ret=$( ssh ${SFTP_ACCOUNT}@${SFTP_ADDR} "test -d $SFTP_DESTINATION >/dev/null 2>&1; echo \$?" )
 
if [ $ret -eq 0 ]
then
  echo "Now go ahead with sftp"
fi

--ahamed