Getting exit status code from 'ftp' command in linux shell Getting exit status code from 'ftp' command in linux shell bash bash

Getting exit status code from 'ftp' command in linux shell


You should be looking for success message from ftp command rather than looking for a status. It's "226 Transfer complete". You can confirm it with ftp manual on your system.

200 PORT command successful.150 Opening ASCII mode data connection for filename.226 Transfer complete.189 bytes sent in 0.145 seconds (0.8078 Kbytes/s)

Here's a sample script.

FTPLOG=/temp/ftplogfileftp -inv <<! > $FTPLOGopen serveruser ftp pwdput filenameclosequit!FTP_SUCCESS_MSG="226 Transfer complete"if fgrep "$FTP_SUCCESS_MSG" $FTPLOG ;then   echo "ftp OK"else   echo "ftp Error: "$OUTfiexit 0


If you need to download something and see if the download succeeded, why don't you use wget? It supports the FTP protocol.

It will report the status of the download with several return codes (quote from man page):

EXIT STATUS   Wget may return one of several error codes if it encounters problems.   0   No problems occurred.   1   Generic error code.   2   Parse error---for instance, when parsing command-line options, the .wgetrc or .netrc...   3   File I/O error.   4   Network failure.   5   SSL verification failure.   6   Username/password authentication failure.   7   Protocol errors.   8   Server issued an error response.


Try the following scripts.

To copy:

#!/bin/bash# cftp.sh# set -xFTPSERVER="$1"FTPPORT="$2"REMOTEDIR="$3"[[ "$REMOTEDIR" ]] || { echo -e "Usage: $0 <ftpserver> <ftpport> <remotedir> [file1] [file2] ..." > /dev/stderr ; exit 1 ; }L=$((BASH_ARGC-3))LOCALFILES=("${BASH_ARGV[@]:0:$L}")RETCODE=0for LOCALFILE in "${LOCALFILES[@]}"do  THISRETCODE=0  [[ -f "$LOCALFILE" ]] || THISRETCODE=1  LOCALDIR="$(dirname "$LOCALFILE")"  LOCALFILENAME="$(basename "$LOCALFILE")"  [[ $THISRETCODE = 0 ]] &&  /usr/bin/ftp -iv "$FTPSERVER" << EOF | grep -q '226 Transfer complete' || THISRETCODE=1    lcd $LOCALDIR    cd $REMOTEDIR    put $LOCALFILENAMEEOF  RETCODE=$((RETCODE+THISRETCODE))doneexit $RETCODE

To move:

#!/bin/bash# mftp.sh# set -xFTPSERVER="$1"FTPPORT="$2"REMOTEDIR="$3"[[ "$REMOTEDIR" ]] || { echo -e "Usage: $0 <ftpserver> <ftpport> <remotedir> [file1] [file2] ..." > /dev/stderr ; exit 1 ; }L=$((BASH_ARGC-3))LOCALFILES=("${BASH_ARGV[@]:0:$L}")RETCODE=0for LOCALFILE in "${LOCALFILES[@]}"do  THISRETCODE=0  [[ -f "$LOCALFILE" ]] || THISRETCODE=1  LOCALDIR="$(dirname "$LOCALFILE")"  LOCALFILENAME="$(basename "$LOCALFILE")"  [[ $THISRETCODE = 0 ]] &&  /usr/bin/ftp -iv "$FTPSERVER" << EOF | grep -q '226 Transfer complete' || THISRETCODE=1    lcd $LOCALDIR    cd $REMOTEDIR    put $LOCALFILENAMEEOF  [[ $THISRETCODE = 0 ]] &&  /bin/rm -f "$LOCALFILE" || THISRETCODE=1  RETCODE=$((RETCODE+THISRETCODE))doneexit $RETCODE

Here are some test cases:

For copying.

$ ./cftp.sh ; echo return code: $?Usage: ./cftp.sh <ftpserver> <ftpport> <remotedir> [file1] [file2] ...return code: 1$ ./cftp.sh ftpserver 21 /mnt/disk4/d0/test ; echo return code: $?return code: 0$ ./cftp.sh ftpserver 21 /mnt/disk4/d0/test cftp.sh mftp.sh ; echo return code: $?return code: 0$ ./cftp.sh ftpserver 21 /mnt/disk4/d0/test *ftp.sh ; echo return code: $?return code: 0$ ./cftp.sh ftpserver 21 /mnt/disk4/d0/test cftp.s ; echo return code: $?return code: 1$ ./cftp.sh ftpserver 21 /mnt/disk4/d0/test cftp.s mftp.s ; echo return code: $?return code: 2$ ./cftp.sh ftpserver 21 /mnt/disk4/d0/tes cftp.sh ; echo return code: $?return code: 1

For moving.

$ ./mftp.sh ftpserver 21 /mnt/disk4/d0/test cftp.sh ; echo return code: $?/bin/rm: cannot remove `cftp.sh': Permission deniedreturn code: 1$ echo foo > /tmp/bar$ ./mftp.sh ftpserver 21 /mnt/disk4/d0/test /tmp/bar ; echo return code: $?return code: 0$ ls -lha /tmp/barls: cannot access /tmp/bar: No such file or directory

Update: Remember to read man 5 netrc