如何改进这个FTP(shell)函数? [英] How to improve this FTP (shell) function?

查看:108
本文介绍了如何改进这个FTP(shell)函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大量的脚本使用以下函数:

 #使用FTP复制文件。 
#在脚本开始时设置的配置
#@param $ 1 = FTP主机
#$ 2 = FTP用户
#$ 3 = FTP用户密码
#$ 4 =源文件名
#$ 5 =目的地目录
#$ 6 =本地目录
doftp(){
log_message_fileINFO启动FTP

ftp_hst = $ 1
ftp_usr = $ 2
ftp_pwd = $ 3
sourcefile = $ 4
destdir = $ 5
locdir = $ 6

ftp -nv $ FTPH << EOF 2> ftp.err。$$
引用USER $ ftp_usr
引用PASS $ ftp_pwd
cd $ destdir
lcd $ locdir
bin

put $ sourcefile
$ b bye
EOF

if [$(wc ftp.err。$$ | cut -d-f8)!= 0] ;然后
log_message_fileERROR问题上传文件:$(cat ftp.err。$$)
else
log_message_fileINFOFTP完成
fi
rm ftp.err。$$
}

它有效,它是否工作,除非ftp失败。幸运的是,对于我来说,脚本非常精确,FTP几乎从不失败。但是,当人们有机会(时间)返回并查看TODO列表中标记的代码时,这就是其中的罕见时刻之一。唯一的问题是,我不太确定如何改善它......我会带你们在那里改变的建议。



一个显而易见的问题是从ftp解析错误,这是一个完全跛脚。但是我也会在函数的其他部分考虑一下:)

值得一提的是这是在AIX服务器上运行的吗?哦,不,我不能使用SFTP:($ / b>

感谢您的任何输入!



ps .: log_message_file只是一个基本的日志记录...在函数中没有效果。

解决方案


  • / li>
  • 良好的变量名称

  • 良好的缩进,您可能需要阅读<< -EOF(' - '是我打算使用的项目使用该功能可让您缩进(必须使用制表符字符)整个here-document以匹配脚本的其余部分。

  • 很好地使用tmp filename和$$ (取决于该函数的使用量,您可能希望将父脚本名称附加到tmp名称的一部分,以进一步消除歧义,但优先级较低)

  • 好用$(cat ftp.err。$$)即实际显示错误信息,而不仅仅是一个像'错误发生'的消息(我总是看到这个,什么错误?什么是味精?!)


  • 你可以扩展使用mput的ftp服务,但是你必须了解你特定的ftp客户端的变幻莫测,并且记下你的自己,每当ftp客户端发生变化时,你需要检查你的mput $ {fileNames}变量仍然按照您的预期工作。

  • 可能考虑改进的一个地方是使用case语句来解析STDERR输出,但是再一次未来的增值收益可能不值得维护成本。

      errMsgs =$(cat ftp.err。$$)

    case $ {errMsgs}in
    * warningStrings *)print找到警告,味精是$ {errMsg} ;;
    * errorStrings *)print找到错误,msg是$ {errMsg} ;;
    * fatalStrings *)pring发现致命错误,无法继续,msg是$ {errMsg} ;;
    esac

    我希望这有助于您。


    I have about a ton of scripts using the following function:

    # 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.$$
    }
    

    It works, does it's job, unless ftp fails. Fortunately for me, the scripts are quite precise and the FTP almost never fails. But this is one of these rare moments when one has the chance (time) to go back and review code marked in the TODO list. Only problem is that I'm not too sure on how to improve it... I'd take you guys recommendations on what to change there.

    One obvious problem is the error parsing from the ftp, which is a totally lame. But I'll also take thoughts in other parts of the function :)

    Worth mentioning this is run in a AIX server? Oh, and no, I cannot use SFTP :(

    Thanks for any input!

    ps.: log_message_file is just a basic logging... has no effect in the function.

    解决方案

    • good documentation
    • good variable names
    • good indentation, you might want to read about <<-EOF (the '-' being the item I intend to highlight with this comment. Using that feature allows you to indent (must use tab chars) the whole here-document to match indenting of rest of script.
    • good use of tmp filename with $$ (depending on how much this function gets used, you might want to append the parent script name as part of the tmp name, to further disambiguate, but low priority)
    • good use of $(cat ftp.err.$$) i.e. actually showing the error message, rather than just a message like 'error occured' (I see this all the time, what error? what was the msg?!)

    • you could extend the ftp service to use mput, but then you have to understand the vagaries of your particular ftp client AND make a note to your self that any time there is a change of ftp client you'll need to check if your mput ${fileNames} variable still works as you expect.

    • possibly the one place to think about improvement would be to use a case statement to parse the STDERR output, but again, the added benefit may not be worth the maintenance cost in the future.

    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

    I hope this helps.

    这篇关于如何改进这个FTP(shell)函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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