我需要从命令行程序中检索退出状态代码。不用担心,我用过$? 但对于ftp,即使它没有连接,也会打开ftp shell,所以我无法理解连接没有发生。
尝试使用此代码了解:
#!/bin/sh
ftp 1234567
OUT=$?
if [ $OUT -eq 0 ];then
echo "ftp OK"
else
echo "ftp Error: "$OUT
fi
exit 0
有任何帮助吗? 谢谢Filippo
答案 0 :(得分:18)
您应该从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
答案 1 :(得分:5)
如果您需要下载某些内容并查看下载是否成功,为什么不使用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.
答案 2 :(得分:2)
尝试以下脚本。
要复制:
#!/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
答案 3 :(得分:1)
一些脚本 -
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
答案 4 :(得分:0)
上次我需要在脚本中使用ftp时,我对此感到非常沮丧,以至于我终于找到了一个获得BSD许可的ftp客户端源代码并简单地对其进行了修改,以便为其提供我需要的行为,并使用它而不是随操作系统提供的版本。
丑陋,但立方体墙上的头顶凹痕的深度开始变得荒谬
答案 5 :(得分:0)
另一种方法是检查您的服务器上是否有文件传输后!
像...一样的东西。
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
如果不存在那么它就没有成功转移!