Check FTP Success-Failure Inside KornShell Script

2019-06-01 18:32发布

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

标签: shell unix ftp ksh
1条回答
▲ chillily
2楼-- · 2019-06-01 19:13

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.

查看更多
登录 后发表回答