检查KornShell脚本中的FTP成功失败

时间:2011-05-19 06:24:45

标签: shell unix ftp ksh

想知道在KornShell(ksh)脚本中检查文件传输协议(FTP)是否成功的正确方法是什么。

1 个答案:

答案 0 :(得分:1)

有很多ftp客户端,其中很多都不一定遵循std返回约定,你必须做一些简单的测试,然后进行相应的编码。

如果你很幸运,你的ftp-client会返回std退出代码并通过man ftp进行记录(你知道手册页吗?)。在这种情况下,0表示成功,任何非零表示某种问题,因此最简单的解决方案就像

if ftp user@remoteHost File remote/path ; then
    print -- 'sucessfully sent file'
else
    print -u2 -- 'error sending file'
fi

(不太确定ftp user@remoteHost file remoteDir是完全正确的,(我现在无法访问客户端并且多年没有使用过ftp(你不应该使用sftp!?; - ))但我在两个例子中使用相同的一致)。

您可能需要更多控制权,因此您需要捕获返回代码。

ftp user@remoteHost File remote/path
ftp_rc=$?

case ${ftp_rc} in
  0 )  print -- 'sucessfully sent file' ;;
  1 )  print -u2 'error on userID' ; exit ${ftp_rc};;
  2 )  print -u2 -- 'no localFile found' ; exit ${ftp_rc};;
esac

我不确定1或2的含义,这些仅仅是说明性的。查看您的man ftp以查看它们是否已记录在案,或者进行简单的测试,一次故意将一个错误发送到ftp以查看它是如何响应的。

如果未使用std错误代码或不一致,则必须捕获ftp输出并检查它以确定状态,如

ftp user@remotehost file remote/path > /tmp/ftp.tmp.$$ 2>&1

case $(< /tmp/ftp.tmp.$$ ) in
  sucess )  print -- 'sucessfully sent file' ;;
  bad_user )  print -u2 'error on userID' ; exit 1 ;;
  no_file )  print -u2 -- 'no localFile found'  ; exit 2;;
esac

我希望这会有所帮助。

相关问题