LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash script: return code ftp (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-return-code-ftp-641042/)

TalkingMarble 05-09-2008 07:50 AM

Bash script: return code ftp
 
Hello,

I'm using the code written below inside a bash script to transfer some files. In order to test the return code from the ftp, i supplied an invalid password. I was surprised by the fact that the ftp was successful. Can anybody tell me why it was successful and how i need to adjust the code?

ftp -inv <<EOF
open IP-address
user USER PASSWORD
lcd /Dir
cd /Dir
binary
put $1
quit
EOF
if [ $? -eq 0 ]
then
Echo “Successful”
Else
Echo “Failed”
fi

TIA

TalkingMarble

pixellany 05-09-2008 09:04 AM

the best way to troubleshoot these situations is to enter the commands manually and look at "$?" at each step.

In your scenario, you are testing the result after issuing a series of ftp commands--not a BASH command. I don't know what happens is that case.

Asy 05-09-2008 09:27 AM

The problem here is that the return code comes from the ftp command. The ftp command itself runs fine, so the return-code is true.

To test a proper ftp your sender shoud put a second file and the you have to test if this file is received. By sending do it the other way round send an extra file if your transport is finished and transport the same file in a new ftp session back. The you are sure the ftp result is ok.

colucix 05-09-2008 09:37 AM

Given the explanation in the previous posts, you can redirect the standard error coming from the ftp command to a file, then check if the file has a size greater than zero and act accordingly, as in
Code:

ftp -inv << EOF 2> ftp.error
  open IP-address
  user USER PASSWORD
  lcd /Dir
  cd /Dir
  binary
  put $1
  quit
EOF
test -s ftp.error && echo Failed || rm ftp.error

Furthermore you can parse the content of the file (if any) and take different actions upon different error messages.


All times are GMT -5. The time now is 02:58 AM.