我需要从命令行程序中检索退出状态代码。不用担心,我用的是 $?。但是对于 ftp,即使它没有连接,它也会打开 ftp shell,所以我无法理解连接没有发生。
试试这个代码来理解:
#!/bin/sh
ftp 1234567
OUT=$?
if [ $OUT -eq 0 ];then
echo "ftp OK"
else
echo "ftp Error: "$OUT
fi
exit 0
有什么帮助吗?谢谢菲利波
您应该寻找来自 ftp 命令的成功消息,而不是寻找状态。它是“226 传输完成”。您可以通过系统上的 ftp 手册进行确认。
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)
这是一个示例脚本。
FTPLOG=/temp/ftplogfile
ftp -inv <<! > $FTPLOG
open server
user ftp pwd
put filename
close
quit
!
FTP_SUCCESS_MSG="226 Transfer complete"
if fgrep "$FTP_SUCCESS_MSG" $FTPLOG ;then
echo "ftp OK"
else
echo "ftp Error: "$OUT
fi
exit 0
如果您需要下载一些东西并查看下载是否成功,为什么不使用wget?它支持FTP协议。
它将使用几个返回码报告下载状态(来自手册页的引用):
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.
尝试以下脚本。
复印:
#!/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
移动:
#!/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
以下是一些测试用例:
用于复制。
$ ./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
为了搬家。
$ ./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
更新:记得阅读 man 5 netrc
一些脚本可以 -
ftp -n $HOST > /tmp/ftp.worked 2> /tmp/ftp.failed <<END_SCRIPT
blah blah
END_SCRIPT
EXITSTATUS=$?
if [ $EXITSTATUS != "0" ]
then
# handle the error...
fi
除了上述并不总是有效 - 大多数 FTP 客户端总是以状态 0 退出。这会导致丑陋的“误报”:文件传输失败,但脚本没有检测到问题。
验证文件传输是否发生的一种方法 - 将其传回:
#!/bin/sh
ftp -n << END_SCRIPT
open $1
user $2 $3
put $4
get $4 retrieval.$$
bye
END_SCRIPT
if [ -f retrieval.$$ ]
then
echo "FTP of $4 to $1 worked"
rm -f retrieval.$$
else
echo "FTP of $4 did not work"
fi
上次我需要在脚本中使用 ftp 时,我对此感到非常沮丧,以至于我终于找到了一个 BSD 许可的 ftp 客户端源并简单地对其进行了修改以使其具有我需要的行为,并使用它而不是提供的版本操作系统。
丑陋,但立方体墙壁上头部凹痕的深度开始变得荒谬
解决此问题的另一种方法是检查您的服务器上是否有文件传输后!
就像是...
if ![ -s "$INPUT_DIR/HOP_PSA_Transactions_$BATCH_ID.csv" ]
then
## No Transactions file
FAIL_TIME=`date +"%d-%m-%Y %H:%M"`
echo "ERROR: File HOP_PSA_Transactions_$BATCH_ID.csv not found @ $FAIL_TIME" >>$LOGFILE_DIR$LOGFILE_NAME
exit $ERR_NO_TRANS_FILE
fi
如果它不在那里,那么它就没有成功传输!