pruebe las siguientes secuencias de comandos.
copiar:
#!/bin/bash
# cftp.sh
# set -x
FTPSERVER="$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=0
for 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 $LOCALFILENAME
EOF
RETCODE=$((RETCODE+THISRETCODE))
done
exit $RETCODE
Para mover:
#!/bin/bash
# mftp.sh
# set -x
FTPSERVER="$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=0
for 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 $LOCALFILENAME
EOF
[[ $THISRETCODE = 0 ]] &&
/bin/rm -f "$LOCALFILE" || THISRETCODE=1
RETCODE=$((RETCODE+THISRETCODE))
done
exit $RETCODE
Éstos son algunos casos de prueba:
Para la reproducción.
$ ./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
Para mudanza.
$ ./mftp.sh ftpserver 21 /mnt/disk4/d0/test cftp.sh ; echo return code: $?
/bin/rm: cannot remove `cftp.sh': Permission denied
return 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/bar
ls: cannot access /tmp/bar: No such file or directory
Actualización: Recuerde leer man 5 netrc
Puede simplificar enormemente el código: if ftp ...; entonces ...; else ...; fi No es necesario comprobar explícitamente $? –