linuxbashftpline-by-line

FTP upload in bash script


I tried to write a bash script which upload a file to an ip address with ftp connection, but i have an issue or something like that. I have a .txt file with a lot of line. Every line have an ip address, nothing else just the ip address. So i want to read the ip address from the text file and use it as a variable. This is my ugly code:

#!/bin/bash


input="example.txt";

user="admin"
pass="12345678"    
filename="example.src"

while IFS= read -r line; do

  connection

done < $input

connection(){
ftp -i -n $line 

user $user $pass

put $filename

bye

}

With the first ip address int the first line the connection and the upload is successful but the other lines got error like this:

Login incorrect
Login failed.
Please login with USER and PASS
Passive mode refused.

Solution

  • Commands of ftp can be written in a here-document.

    #!/bin/bash
    
    connection(){
    ftp -i -n $line << EOS
    user $user $pass
    put $filename
    bye
    EOS
    }
    
    input="example.txt";
    
    user="admin"
    pass="12345678"    
    filename="example.src"
    
    while IFS= read -r line; do
    
      connection
    
    done < $input