0

I have stored all the filenames which i want to download from the SFTP server in a file. This file is stored in my local machine.

I am trying to pass the contents of the file to an array.

Is it possible to pass this array as an argument to mget?

something like mget $my_array where my_array has the list of filenames.

1 Answers1

1

First of all mind that mget is an lftp command, so it need to be used with the -c switch of lftp.

Now, considering that the array contains full-paths to files you could do

lftp -c mget "${array[@]}"

as anonymous user to get these files.

Example

array=( "ftp://ftp.redhat.com/redhat/brms/5.3.1/SHA256SUM" "ftp://ftp.redhat.com/redhat/brms/5.3.1/brms-p-5.3.1.GA-src.zip" )
lftp -c mget "${array[@]}"

would fetch you two files in question.


Why double quote ${array[@]}?

When the expansion occurs within double quotes, each parameter expands to a separate word, so you can tackle word splitting for file names with spaces in them (though rare).


Edit (Remeber this is not in an lftp session but a bash session at localhost)

Suppose you only have the filenames in the bash array like below

array=( "SHA256SUM" "brms-p-5.3.1.GA-src.zip" )

First do :

array=( "${array[@]/#/ftp://ftp.redhat.com/redhat/brms/5.3.1/}" )
# We have just added the ftp server name + path before every file name in array
lftp -c mget "${array[@]}" # Download the files just like that
sjsam
  • 21,411
  • 5
  • 55
  • 102
  • I don't have the absolute path in the array. I have the filenames only. – Debajyoti Majumdar Sep 01 '16 at 13:39
  • @DebajyotiMajumdar : Do you have the address of the server + folder where the files are located? – sjsam Sep 01 '16 at 13:41
  • @DebajyotiMajumdar : I'm not sure if you're already in an lftp session? Please update the question with this detail. – sjsam Sep 01 '16 at 13:44
  • i am in an lftp session. – Debajyoti Majumdar Sep 01 '16 at 13:54
  • @DebajyotiMajumdar : If you have the server name and working directory, then you might do the trick I'm about to mention in my edit. Also, mget supports only globbing to my knowledge - for example `mget file*` and once within lftp you can't use variables . – sjsam Sep 01 '16 at 14:10