在KornShell脚本中检查FTP成功 - 失败 [英] Check FTP Success-Failure Inside KornShell Script

查看:168
本文介绍了在KornShell脚本中检查FTP成功 - 失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

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



如果你幸运的话,你的ftp-client确实会返回std退出代码,并通过 man ftp 来记录它们(你知道man page?)。在这种情况下,0表示成功,任何非零表示某种问题,所以最简单的解决方案就像

 如果ftp用户@ remoteHost文件远程/路径;然后
print - '成功发送文件'
else
print -u2 - '发送文件错误'
fi

(不太确定 ftp user @ remoteHost文件remoteDir 是否正确,(我没有现在有权访问客户端,并且多年以来一直没有使用过ftp(你不应该使用sftp !?--)),但我在这两个示例中使用相同的方式来保持一致。)



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

  ftp用户@remoteHost文件远程/路径
ftp_rc = $?

case $ {ftp_rc} in
0)print - '成功发送文件';;
1)print -u2'userID'错误;退出$ {ftp_rc} ;;
2)print -u2 - '找不到localFile';退出$ {ftp_rc} ;;
esac

我不确定1或2的含义,只是说明性的。看看你的 man ftp 来查看它们是否有文档记录,或者做一个简单的测试,故意一次给ftp一个错误,看它是如何响应的。



如果没有使用std错误代码或不一致,那么您必须捕获ftp输出并检查它以确定状态,例如

  ftp user @ remotehost file remote / path> /tmp/ftp.tmp.$$ 2>& 1 
$ b $ case $(< /tmp/ftp.tmp.$$)in
sucess)print - '已成功发送文件';;
bad_user)print -u2'userID'错误; 1号出口;;
no_file)print -u2 - '找不到localFile'; 2号出口;;
esac

我希望这有助于您。


Wondering what are some of the right ways to check whether File Transfer Protocol (FTP) succeeded or not inside the KornShell (ksh) script.

解决方案

There are so many ftp-clients AND many of there do not necessarily follow std return conventions that you have to do some simple tests, and then code accordingly.

If you're lucky, your ftp-client does return std exit codes and they are documented via man ftp (You do know about man pages?). In that case, 0 means success and any non-zero indicates some sort of problem, so the very easiest solution is something like

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

( not quite sure that the ftp user@remoteHost file remoteDir is exactly right, (I don't have access to a client right now and haven't used ftp in years (shouldn't you be using sftp!? ;-) ) but I use the same in both examples to be consistent).

You probably want a little more control, so you need to capture the return code.

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

I'm not certain about the meaning of 1 or 2, these are meant to be illustrative only. Look at your man ftp to see if they are documented, OR do a simple test where deliberately give one error at a time to ftp to see how it is responding.

If std error codes are not used or are inconsistent, then you have to capture the ftp output and examine it to determine status, something like

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

I hope this helps.

这篇关于在KornShell脚本中检查FTP成功 - 失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆