Sftp : not able to print the echo statements after the sftp transfer

I had the below sftp script working perfectly but the problem is I am not able to send the echo statements .

#!/bin/sh
echo "Starting to sftp..."
sftp admin@myip << END_SCRIPT
cd /remotepath/
lcd /localpath/
mget myfiles*.csv
bye
END_SCRIPT
echo "Sftp successfully."
echo
echo "program ended"
echo

Now the files has been successfully copied but the echo statments were not printed after the completion of sftp. i.e., "Sftp successfully" and "program ended" were not printed...

Could anyone please let me know what went wrong and how to print the echo statments at the end.

Thanks in advance..

Regards,
J

Hmm, works here.

The second END_SCRIPT, which ends the here-document, must start at the beginning of the line, no leading blanks or tabs are allowed. But you should get a warning from the shell, if this is not the case.

1 Like

Thanks sir...

I have verified and executed the script once again.. but no luck.. As you said.. second END_SCRIPT here statement started exactly at the beginning of the file and not sure that it executed for you..

Are there any other possibilities ??

Regards,
J

Since it works here, there must be something in the real script, which is not present in your example.

Try to replace "END_SCRIPT" with something different, shorter, like "EOF" for example (maybe there is a typo there). Try to execute sftp step by step manually and see, if something unusual happens, which your script does not take into account. It's a try-and-error approach, I'm afraid.

1 Like

I'm not sure this is right. << END_SCRIPT
Try <<END_SCRIPT

Well, SFTP is a very different client to FTP. I don't think that SFTP will accept in-stream commands (well, that's what MVS used to call them) You will need to use the batch mode of SFTP.

my-stuff.sftpb

cd /remotepath/
lcd /localpath/
mget myfiles*.csv
bye

Script

#!/bin/sh
echo "Starting to sftp..."
sftp -b my-stuff.sftpb admin@myip
if [ $? -eq 0] 
then
   print "Sftp successfully.\n\nprogram ended\n"
else
   print "Sftp error.\n\nprogram abended\n"
fi

Of course, this is wholly automated, so you cannot pause for passphrase entry or anything. The server fingerprint must already be in known_hosts, but you've probably tried a manual SFTP or SSH which will establish that for you anyway.

I hope that this helps.

Robin
Liverpool/Blackburn
UK

1 Like

I've found most sftp clients are able to accept here-documents.

1 Like

Thanks all ...

Now I am able to get the echo statement messages...

And there is another problem ...

I am sftp'ng files from remote server to local server. But the timestamps(date also) were replaced with the present time of the local server and in reverse order.

Eg. on remote server

$ ls -ltr
total 20672
-rw-r--r--   1 john      other      10471630 Mar 19  12:56  file1.txt
-rw-r--r--   1 john      other      45496       Mar 19  13:37  file2.txt
-rw-r--r--   1 john      other      45512        Mar 24 13:42  file3.txt

But after sftping files were replaced with present date and timestamp ..Also files are received in reverse order.But I am looking for the same order as in remote server.
At present I am receiving with below time stamps

-rw-r--r-- 1 john    users   45512       2013-03-28 03:43 file3.txt
-rw-r--r-- 1 john   users    45496       2013-03-28 03:43 file2.txt
-rw-r--r-- 1 john   users    10471630 2013-03-28 03:43  file1.txt

Could you please help with appropriate command ?

Thanks in advance...

Regards,
J

If you look in the man page and find the section for interactive commands, you may have the option for a -P on the get sub-command that collects permissions and access time.

If not, can you SSH to the server and archive the files with tar? If so you can SFTP the tar files and when extracted, you can preserve modification times then.

Do either of these help?

Robin

1 Like

I guess it's better to tar and then sftp the files and then extract the tar file..

Thanks alot rbatte1.....

Well, using get -P would save coding, elapse time and disk space if it is available. Have you checked the man page for SFTP on your system?

Robin

Directories don't really have any dependable order in particular, never did.