我有大量使用以下函数的脚本:
# Copies files over using FTP.
# Configurations set at the beggining of the script
# @param $1 = FTP Host
# $2 = FTP User
# $3 = FTP User password
# $4 = Source file name
# $5 = destination directory
# $6 = local directory
doftp() {
log_message_file "INFO" "Starting FTP"
ftp_hst=$1
ftp_usr=$2
ftp_pwd=$3
sourcefile=$4
destdir=$5
locdir=$6
ftp -nv $FTPH << EOF 2> ftp.err.$$
quote USER $ftp_usr
quote PASS $ftp_pwd
cd $destdir
lcd $locdir
bin
put $sourcefile
bye
EOF
if [ "$(wc ftp.err.$$|cut -d" " -f8)" != 0 ] ; then
log_message_file "ERROR" "Problem uploading files: $(cat ftp.err.$$)"
else
log_message_file "INFO" "FTP finished"
fi
rm ftp.err.$$
}
它可以工作,它可以工作,除非 ftp 失败。对我来说幸运的是,脚本非常精确,FTP 几乎不会失败。但这是人们有机会(时间)回去查看待办事项列表中标记的代码的罕见时刻之一。唯一的问题是我不太确定如何改进它......我会向你们提出关于那里应该改变什么的建议。
一个明显的问题是从 ftp 解析错误,这是一个完全蹩脚的问题。但我也会考虑该函数的其他部分:)
值得一提的是,这是在 AIX 服务器上运行的吗?哦,不,我不能使用 SFTP :(
感谢您的任何意见!
ps.:log_message_file只是一个基本的日志记录...在功能上没有任何作用。
- 良好的文档
- 好的变量名
- 良好的缩进,您可能想阅读有关
- 很好地使用 tmp 文件名和 $$ (取决于此函数的使用程度,您可能希望将父脚本名称附加为 tmp 名称的一部分,以进一步消除歧义,但优先级较低)
充分利用 $(cat ftp.err.$$) 即实际显示错误消息,而不仅仅是像“发生错误”这样的消息(我一直看到这个,什么错误?消息是什么?!)
您可以扩展 ftp 服务以使用 mput,但是您必须了解特定 ftp 客户端的变化无常,并提醒自己,每当 ftp 客户端发生更改时,您都需要检查您的 mput 是否{fileNames} 变量仍然按您的预期工作。
考虑改进的一个地方可能是使用 case 语句来解析 STDERR 输出,但同样,额外的好处可能不值得将来的维护成本。
errMsgs="$(cat ftp.err.$$)"
case "${errMsgs}" in
*warningStrings* ) print "warning found, msg was ${errMsg} ;;
*errorStrings* ) print "error found, msg was ${errMsg} ;;
*fatalStrings* ) pring "fatal error found, can't continue, msg was ${errMsg} ;;
esac
我希望这有帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)