LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash script problem with ftp session exiting the script early (https://www.linuxquestions.org/questions/programming-9/bash-script-problem-with-ftp-session-exiting-the-script-early-790693/)

edomingox 02-22-2010 12:43 AM

Bash script problem with ftp session exiting the script early
 
I got this script that backs up a server and then keeps only the last 10 backups and removes the rest. Problem is, the part where it deletes any backups older than the last 10, it never reaches that. I'm guessing it is because the ftp session somehow exits the script entirely, especially if there is a problem with the ftp session. But just to make sure, I'm wondering if you all can look at the code to see if I am making it exit accidentally.

Here's the code. I left out data like ip address and directory/file names for my security purposes.

Code:

#ftp in and grab the file
ftp -in xx.xx.xx.xx << EOF
user user password
bin
cd /export/home/backup
lcd /data1
lcd $backupDir
lcd $backupDirNext
get file.cfg
get file.dmp
close
bye
EOF

#remove old backups here


colucix 02-22-2010 02:20 AM

Apparently the ftp code is correct. The close statement is not necessary, since you don't need to keep the ftp session open for further operations, but that cannot be an issue. You can try to save the stdout and stderr of the ftp command, using the verbose option for checking, e.g.
Code:

ftp -in -v xx.xx.xx.xx << EOF > ftpout.log 2> ftperr.log
or eventually try a different tool like curl for example.
By the way, are the files you want to retrieve actually downloaded to the local machine?

edomingox 02-22-2010 06:21 AM

ya. the files are there. and when i check if it was transferred, it is. I'm not sure what could be causing this. It could be the get command. I can try using curl and see what happens.

edomingox 02-22-2010 07:06 AM

I added the ftpout.log and ftperror.log and the error log shows nothing and the out log shows nothing other than navigating through the directories. The files were transferred fine, but the anything after that does not execute. I had an echo line after EOF and it never executed. So I'm still stumped on why it exits.

colucix 02-22-2010 08:21 AM

Quote:

Originally Posted by edomingox (Post 3872493)
I added the ftpout.log and ftperror.log and the error log shows nothing and the out log shows nothing other than navigating through the directories. The files were transferred fine, but the anything after that does not execute. I had an echo line after EOF and it never executed. So I'm still stumped on why it exits.

Ok. First check if you see the following line at the end of ftpout.log:
Code:

221 Goodbye.
this means that the remote server answered to your greetings and all the ftp commands have been executed properly. If this is the case, I suspect you've placed the closing EOF keyword of the here document at the wrong place: take in mind that it must be at the beginning of the line (no leading spaces are allowed, nor trailing hidden/control characters). If the syntax is not correct the end of the here document cannot be never reached and every subsequent line of code is passed as standard input to the ftp command (which has already closed the connection) and ignored.

edomingox 02-23-2010 05:39 AM

That worked. I actually had a space after EOF. I also had a #comment after it too. I didn't think that would of affected it when I retyped it on this post so I didn't include it.

Thanks for your help.


All times are GMT -5. The time now is 04:29 PM.