You should not be injecting insecure plaintext passwords into sftp with the expect brute-forcing tool. sftp allows you to use keys, which will make your life much, much simpler, since you can script it directly, run it and wait, instead of having to micromanage every line it prints and reads. Also far more secure, since your scripts will not need to contain your password in plaintext anymore!
hope it helps but put your ssh keys to server as @Corona688 mentioned.
sftp $SFTP_USER@$SERVER <<EOF | grep -q 'No such file'
cd $SFTP_RDIR
ls $SFTP_RFILE
bye
EOF
if [ $? -eq 1 ] ; then
echo 'no such remote file'
else
sftp $SFTP_USER@$SERVER <<EOF
cd $SFTP_RDIR
get $SFTP_RFILE
bye
EOF
fi
The | grep -q 'No such file' might be useless because the error likely goes to stderr, while the pipe reads from stdout.
You need some additional file descriptor magic such as 2>&1 | grep ... (merge stderr with stdout) or 2>&1 >/dev/null | grep ... (merge stderr with stdout then suppress stdout, i.e. capture only stderr).
Further the exit status is reverse: if grep finds something (an error), the exit status is zero!
The following runs directly on the get stuff, lets grep print an error,
and adds an "okay" otherwise.
sftp $SFTP_USER@$SERVER <<EOF 2>&1 | grep 'No such file'
cd $SFTP_RDIR
get $SFTP_RFILE
bye
EOF
if [ $? -eq 1 ]; then
# grep did not find an error
echo "okay"
fi