0

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

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

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

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';

which gives me

/Some Folder/BigFile.mov

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'"

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'

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 ...

Dan
  • 1,257
  • 2
  • 15
  • 31
  • Use a function instead of an alias and the rest should come easily. – tripleee Mar 03 '16 at 12:22
  • Sorry, I don't have a lot of bash scripting knowledge; how would you write the function to include the processed URL within the `lftp` command? – Dan Mar 03 '16 at 12:34

1 Answers1

1

Something like this?

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).

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'.)

Community
  • 1
  • 1
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Brilliant! It works! I only mentioned the function within an alias because I saw this: http://stackoverflow.com/questions/941338/how-to-pass-command-line-arguments-to-a-shell-alias ; Didn't realize the function was enough by itself. Thanks! – Dan Mar 03 '16 at 14:45
  • One more question ... can that function be written in one line? Line breaks are significant in bash, right? – Dan Mar 03 '16 at 17:59
  • Line breaks can be replaced with a semicolon. You don't need one after the opening brace. Unclear to me why you would have such a requirement, though. – tripleee Mar 04 '16 at 08:16
  • I eventually asked [Why would I create an alias which creates a function?](https://stackoverflow.com/questions/57239089/why-would-i-create-an-alias-which-creates-a-function) and got some speculative answers. – tripleee Oct 03 '19 at 03:47