从linux shell中的'ftp'命令获取退出状态代码

Pos*_*ssa 10 linux ftp bash shell exit-code

我需要从命令行程序中检索退出状态代码.不用担心,我用过$?但对于ftp,即使它没有连接,也会打开ftp shell,所以我无法理解连接没有发生.

试试这段代码吧:

#!/bin/sh

ftp 1234567
OUT=$?
if [ $OUT -eq 0 ];then
   echo "ftp OK"
else
   echo "ftp Error: "$OUT
fi

exit 0
Run Code Online (Sandbox Code Playgroud)

有帮助吗?谢谢菲利波

Ruc*_*chi 19

您应该从ftp命令中寻找成功消息,而不是寻找状态.这是"226转移完成".您可以在系统上使用ftp manual确认.

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)
Run Code Online (Sandbox Code Playgroud)

这是一个示例脚本.

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
Run Code Online (Sandbox Code Playgroud)


And*_*ini 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.
Run Code Online (Sandbox Code Playgroud)