LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash and sftp (https://www.linuxquestions.org/questions/linux-newbie-8/bash-and-sftp-736661/)

solar05 06-30-2009 06:45 AM

Bash and sftp
 
Hello, I need to copy every hour some files from local directory to remote vis sftp.
I open 1 session for all files and copy them to remote host with mput.
But if the copying would be wrong - for example connection would be lost. How I can know abouit it in the Bash.

PHP Code:

#!/bin/bash
OUTDIR="/outdir"
TARGET_HOST=localhost
TARGET_USER
=root
TARGET_DIR
="/indir"
DATE_LOG=`date +%R-%d-%m-%Y`
FTPLOG="/log/$DATE_LOG.log"
SLEEP_INTERVAL=60
while [ ]; do
sftp -/dev/fd/0 $TARGET_USER@$TARGET_HOST>>$FTPLOG 2>&1<<EOT
lcd $OUTDIR
mput 
*.zip
chmod 666 
*.zip
exit
EOT
sleep $SLEEP_INTERVAL
done 


nuwen52 06-30-2009 07:14 AM

First, in timed things like this, you might consider building this as a cron job. It can make the script less complex and gets rid of the need of another programming running all of the time.

Now then, to answer your actual question. After:
sftp -b /dev/fd/0 $TARGET_USER@$TARGET_HOST>>$FTPLOG 2>&1<<EOT
try checking the exit status of sftp. Like:
Code:

RET=$?
if [ $RET -eq 0 ]
then
    it worked
else
    it didn't
fi

"$?" is the return code for the previously executed process. Usually, if it's zero, everything is fine.

solar05 06-30-2009 11:44 PM

Quote:

Originally Posted by nuwen52 (Post 3591372)
First, in timed things like this, you might consider building this as a cron job. It can make the script less complex and gets rid of the need of another programming running all of the time.

Now then, to answer your actual question. After:
sftp -b /dev/fd/0 $TARGET_USER@$TARGET_HOST>>$FTPLOG 2>&1<<EOT
try checking the exit status of sftp. Like:
Code:

RET=$?
if [ $RET -eq 0 ]
then
    it worked
else
    it didn't
fi

"$?" is the return code for the previously executed process. Usually, if it's zero, everything is fine.

Yes, it may be use when the connection will fail, but if while during
sftp session working on remote computer and there script could'nt copy some files or some permissions incorrect. And that its why sftp will give
Code:

RET=$?
TRUE but files didn't copy to remote host. How I can see that all files copyed successfully?

chrism01 07-01-2009 12:37 AM

If you want to know the status of the copy and chmod on a file-by-file basis, you'll need to replace the while loop with

for file in *.zip
sftp ....
lcd ..
put $file

ie perform the copy, then exit and check the status, then do another sftp cxn and run the chmod cmd and check it.

solar05 07-01-2009 01:28 AM

Quote:

Originally Posted by chrism01 (Post 3592497)
If you want to know the status of the copy and chmod on a file-by-file basis, you'll need to replace the while loop with

for file in *.zip
sftp ....
lcd ..
put $file

ie perform the copy, then exit and check the status, then do another sftp cxn and run the chmod cmd and check it.

Yes it's good desicion, but I want to copy all files in 1 sftp session, and don't use per each file each sftp session

chrism01 07-01-2009 01:36 AM

Then you can't do it... in SFTP
Look, you can either send & chmod all the files in one go, then use an ssh session to do a remote 'ls -l' (assuming you can ssh in), or you check each operation as you go.
Your choice.

Another way that might do what you want with one session would be to use one of the Perl SSH modules. I think you'd be able to do an scp within the ssh session, as well as the chmod cmd, and check as you go.


All times are GMT -5. The time now is 03:45 AM.