Search code examples
bashlftp

pass an array as argument to mget command


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.


Solution

  • 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