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.
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.
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..
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.
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.
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
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.