3

I have several files named as this:

TEST_YYYYMMDD.txt

I need to upload them via ftp, so to get the list of files I run:

files=($(ls | grep -i -E 'TEST_[0-9]{8}.txt'))

Now that I got the file list, how can I put them via ftp?

function upload() {
list="$1"
length=${#list[*]}
for ((i=0;i<=$(($length - 1)); i++))
do 
 ftp -n host << EOF
 quote USER user
 quote PASS password
 prompt
 put ${list[$i]}
 quit
 EOF
done
}

This would open a connection for each file.
Anyone could give me a better implementation?

mput I don't know how to use it. .TEST_* as wildcard isn't safe for me.

Thank you


Here how I've solved it

function upload() { 
list="$1" 
$user="username"
$password="password"
length=${#list[*]}
echo "open host user $user $password 
 binary 
 cd folder" > tmp/ftp.$$
 for ((i=0;i<=$(($length- 1)); i++))
 do 
echo "put ${list[$i]}" >> tmp/ftp.$$ 
done
echo "quit" >> tmp/ftp.$$
 ftp -ivn < tmp/ftp.$$ 
rm /tmp/ftp.$$ 
}

1 Answers1

2

Loop only the put command.

(
 echo quote USER user
 echo quote PASS password
 echo prompt
 for ((i=0;i<=$(($length - 1)); i++))
 do
  echo put ${list[$i]}
 done
 echo quit
) | ftp -n host
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992