bash脚本以使用ftp路径运行lftp [英] bash script to run lftp with ftp path

查看:183
本文介绍了bash脚本以使用ftp路径运行lftp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个bash别名/函数,该别名/函数采用ftp路径并运行lftppget文件.

I'm trying to write a bash alias/function that takes an ftp path and runs lftp to pget the file.

给出一个远程路径,其空格编码为%20,例如:

Given a remote path, with spaces encoded as %20 like:

sftp://ftp.example.com/Some%20Folder/BigFile.mov

我有以下代码段来清理URL的%20和服务器部分:

I have this snippet to clean up the %20 and server part of the URL:

echo $1 | sed 's/%20/ /g' | sed 's/sftp:\/\/ftp.example.com//g';

这给了我

/Some Folder/BigFile.mov

现在我需要像这样通过lftp运行它:

Now I need to run this through lftp like this:

lftp sftp://ftp.example.com -u user,pass -e "pget -cn10 '/Some Folder/BigFile.mov'"

我想为该命令创建一个函数别名,我们将其命名为lget,这样我就可以使用原始URL:

I want to create an alias with a function to this command, let's call it lget, so I can just use the original URL:

lget "sftp://ftp.example.com/Some%20Folder/BigFile.mov"

此处的解决方案应类似于

The solution here should like something like

alias lget='function _lget(){ lftp ... };_lget'

但是我完全不知道如何在函数中包含参数,如何通过sed处理参数,以及如何处理嵌套的引号.

But I get completely lost as to how to include the argument in the function, have it processed through sed, and how to handle the nested quotation marks.

我可能需要一些反引号...

I probably need some backticks ...

推荐答案

像这样吗?

lget () {
    local filename=${1#sftp://ftp.example.com}
    lftp sftp://ftp.example.com -u user,pass -e "pget -cn10 '${filename//%20/ }'"
}

我不明白为什么您只想将有用的名称分配给一个简单地调用该函数的别名(为什么这在某些圈子中似乎是正常的),为什么您只想用一个无用的名称来创建一个函数?我不知道的原因).

I don't see why you would want to create a function with a useless name only to be able to assign the useful name to an alias which simply calls the function (though this seems to be the norm in some circles, for reasons unknown to me).

您不需要sed,因为Bash包含用于字符串替换的内置函数.如果您需要将它移植到缺少${variable//global/replacement}功能(和local关键字)的POSIX shell中,请小心

You don't need sed because Bash contains built-in functions for string substitution. If you need this to be portable to POSIX shell, which lacks the ${variable//global/replacement} functionality (and the local keyword), take care to properly quote the string you pass to sed. (The ${variable#prefix} syntax to retrieve the value of a variable with a prefix removed is available in POSIX shell. Oh, and for the record, a single sed script can perform multiple substitutions; sed -e 's/foo/bar/' -e 's/baz/quux/g'.)

这篇关于bash脚本以使用ftp路径运行lftp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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