Dynamic command execution with lftp - multiple commands Dynamic command execution with lftp - multiple commands shell shell

Dynamic command execution with lftp - multiple commands


First build a string that contains the list of lftp commands. Then call lftp, passing the command on its standard input. Lftp itself can redirect the output of a command to a file, with a syntax that resembles the shell.

list_commands=""for dir in "${myarray[@]}"; do  list_commands="$list_commands  cd \"$dir\"  nlist >\"$dir.txt\"  cd .."donelftp <<EOFopen -u $username,$password $site$list_commandsbyeEOF

Note that I assume that the directory names don't contain backslashes, single quotes or globbing characters. Add proper escaping if necessary.

By the way, to read lines from a file, see Why is while IFS= read used so often, instead of IFS=; while read..?. You might prefer to combine reading from the list of directories and building the commands:

list_commands=""while IFS= read -r dir; do  list_commands="$list_commands  cd \"$dir\"  nlist >\"$dir.txt\"  cd .."done <directory_list.txt