[bash] jump from one txt file to another

Hi all,

I need to create a bash script that reads a txt file on a remote (Windows 2003) server, gets the IP-addresses out of it and then fetches the folders to copy out of another txt file. (all files are auto-generated) The IP addresses that don't have a folder_list file should be ignored. At the moment I can get my txt via FTP and read it out.
I would already be very happy if I had a script that could ECHO the folders based on the ftpservers.txt file, later on I will use this to transfer the folders via FTP.
It's kind of complicated, but please check the attachment (JPEG)

This is an example of the layout of the main file: ftpservers.txt ; contains the IP-addresses, usernames and password. Only the 1st,4th,7th,10th,13th,16th,... line are important in the first place.

line 1 = IP-address
line 2 = FTP-user
line 3 = FTP-pwd
line 4 = IP-address
line 5 = FTP-user
... etc ...
line 33 = FTP-user
line 34 = FTP-pwd
line 35 = IP-address
line 36 = FTP-user
line 37 = FTP-pwd
#-END-

In the same folder there are some files (4 at the moment) named for example 212.114.xxx.xxx_folderlist. Where the first part is the IP address. These files contain the folders

vabdocshop.xxx.de\files
intranetdocshop.xxx.de\files
fleet.xxx.de\doccshop\files

Thanks in advance to help me out here.
Please ask me if you need more information.

Regards,
Laurens

no one?

We can't write the whole script for you, but as a first step you can try this, it prints the files from ftpservers.txt:

awk 'NR%3==1{a[++i]=$0}END{for(f in a){print a[f] "_folderlist.txt"}}' ftpservers.txt

Regards

Thanks for your reply Franklin,
It looks like a solid line of code, though, it gives me this output:

...
drwxrwsr-x    2 503      504          4096 Aug 22  2005 back up boekhouding
-rw-rw----    1 503      504           738 May 11 16:12 ftpservers.txt
226 Directory send OK.
221 Goodbye.
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt

This is my bash script so far:

ftp -n -v immserv1.xxx.XXX.be << EOT
ascii
user username pass
prompt
cd BACKUP
get ftpservers.txt
ls
EOT
#iplist=$(sed -n 1~3p ftpservers.txt)
awk 'NR%3==1{a[++i]=$0}END{for(f in a){print a[f] "_folderlist.txt"}}' ftpservers.txt
#rm ftpservers.txt

I would also need a line that gets all the files with the .TXT extension.

Thanks in advance

Btw, I understand asking you guys to write the whole script is quite impossible but I would already be glad with the first steps. step by step...

Can you post a part of the file ftpservers.txt and the desired output within code tags?

Regards

The file: ftpservers.txt

80.xx.xx.76              -> IP server
ftpmac28                 -> username
passO#lanL              -> password
212.xx.xx.52             -> IP server
ftpmac28                 -> username
passO#lanL              -> password
212.xx.xx.61
ftpmac28
passO#lanL
212.xx.xx.62
ftpmac28
passO#lanL
212.xx.xx.17
ftpmac28
passO#lanL
212.xx.xx.10
ftpmac28
passO#lanL
212.xx.xx.75
ftpmac28
passO#lanL

...etc...

For the _folderlist.txt files; they look like this:

docshop.xx.be\files
extranetdocshop.xx.be\files
fleet.xx.be\doccshop\files

The thing I would like to have for now is that the folders would be displayed.

Thus, when I execute my bash script ./bash.sh , I should get something like:

212.124.56.75
docshop.xx.be\files
extranetdocshop.xx.be\files
fleet.xx.be\doccshop\files
212.124.56.76
-nothing-
212.124.56.101
project.xx.be\html\upload
project.xx.be\html\attachement
212.124.56.113
-nothing-

...etc...

The output of this shouldn't necessarily be the same (layout) but the most important is that it shows the folder paths.

I hope you understand now what it should be doing.

Thanks in advance!

Regards,
Laurens

awk 'NR%3==1{a[++i]=$0}END{for(f in a){print a[f] "_folderlist.txt"}}' ftpservers.txt

I'm confused, do you get this output when you run the above command?

...
drwxrwsr-x    2 503      504          4096 Aug 22  2005 back up boekhouding
-rw-rw----    1 503      504           738 May 11 16:12 ftpservers.txt
226 Directory send OK.
221 Goodbye.
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt

As a matter a fact, yes! This is what I get -exactly- :

_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt
_folderlist.txt

I guess for each IP it finds it gives a _folderlist.txt output... (?)

btw, I already have this piece of code that gives me the IP's back

ftp -n -v immserv1.xx.xx.be << EOT
ascii
user UN password
prompt
cd BACKUP
get ftpservers.txt
ls
EOT
iplist=$(sed -n 1~3p ftpservers.txt)
for LINE in $iplist; do

     echo "$LINE"
#     ping -c 1 $LINE

done

This is step one. Next step is to display the folders that match with the IP's _folderlist. I've added a printscreen.

That's why I'm confused, this is the ouput I get:

$ cat file
80.xx.xx.76
ftpmac28
passO#lanL
212.xx.xx.52
ftpmac28
passO#lanL
212.xx.xx.61
ftpmac28
passO#lanL
212.xx.xx.62
ftpmac28
passO#lanL
212.xx.xx.17
ftpmac28
passO#lanL
212.xx.xx.10
ftpmac28
passO#lanL
212.xx.xx.75
ftpmac28
passO#lanL
$ awk 'NR%3==1{a[++i]=$0}END{for(f in a){print a[f] "_folderlist.txt"}}' file
212.xx.xx.62_folderlist.txt
212.xx.xx.17_folderlist.txt
212.xx.xx.10_folderlist.txt
212.xx.xx.75_folderlist.txt
80.xx.xx.76_folderlist.txt
212.xx.xx.52_folderlist.txt
212.xx.xx.61_folderlist.txt

Have I missed something?

awk 'NR%3==1{a[++i]=$0}END{for(f in a){print a[f] "_folderlist.txt"}}' ftpservers.txt

This was in my bash script and the output of it was in my previous post. It would be weird I have a different output?

At the moment I don't have access to the servers, I will try again tomorrow. Can you maybe suggest a bash solution for the second step (concerning the _folderlists)? I will try it next thing tomorrow. Thx in advance

Regards,
Laurens

This should give you the filenames and contents of the files:

awk 'NR%3==1{a[++i]=$0}END{for(f in a){print a[f];system("cat " a[f] "_folderlist.txt")}}' ftpservers.txt

The output should be like:

212.124.56.75
docshop.xx.be\files
extranetdocshop.xx.be\files
fleet.xx.be\doccshop\files
212.124.56.76
212.124.56.101
project.xx.be\html\upload
project.xx.be\html\attachement
212.124.56.113

Thanks! I will try that asap and report to you

Darned... It gives me this output for every IP address.

80.xx.xx.96
_folderlist.txt: No such file or directory
80.xx.xx.146
_folderlist.txt: No such file or directory
212.xx.xx.172
_folderlist.txt: No such file or directory
...etc...

I made a new script with only this inside:

awk 'NR%3==1{a[++i]=$0}END{for(f in a){print a[f];system("cat " a[f] "_folderlist.txt")}}' ftpservers.txt

EDIT: I found it! There had to be a conversion from a windows txt format to unix txt format first, which I achieved like this

awk '{ sub("\r$", ""); print }' ftpservers.txt > unixfile.txt
awk 'NR%3==1{a[++i]=$0}END{for(f in a){print a[f];system("cat " a[f] "_folderlist.txt")}}' unixfile.txt

Now I would need something like an IF clause, if there is a folderlist I have to transfer the files via FTP. What isn't a problem, it's only the if clause...

Thanks!
Laurens

Really weird, I've tested this command on a Debian and on a HP Unix system , it works properly on both systems with an echo command so it should work with a cat command. With the given input file I get this:

$ awk 'NR%3==1{a[++i]=$0}END{for(f in a){print a[f];system("echo " a[f] "_folderlist.txt")}}' file
212.xx.xx.52
212.xx.xx.52_folderlist.txt
212.xx.xx.61
212.xx.xx.61_folderlist.txt
212.xx.xx.62
212.xx.xx.62_folderlist.txt
212.xx.xx.17
212.xx.xx.17_folderlist.txt
212.xx.xx.10
212.xx.xx.10_folderlist.txt
212.xx.xx.75
212.xx.xx.75_folderlist.txt
80.xx.xx.76
80.xx.xx.76_folderlist.txt
$

Indeed, I hope you saw it works perfectly now! Only now I need to have an IF clause for the IP's where there's a folderlist for. See previous post.
Muchos gracias

Maybe you can check this thread to get an idea how to use a loop within the ftp command:

Regards

FTP is not really a problem here, though I will check out the thread. But what I would need here is:

awk '{ sub("\r$", ""); print }' ftpservers.txt > unixfile.txt
awk 'NR%3==1{a[++i]=$0}END{for(f in a){print a[f];system("cat " a[f] "_folderlist.txt")}}' unixfile.txt 

If _folderlist.txt exists then
 ftp myserver 
 user UN PW
 **transfer folders** 
else 
 **show: No such file or directory (or something like that)**
fi
done

Thanks in advance

You can give this a try. The awk command prints the ipaddress and the filename separated by a space.
The 1st while loop reads the ipaddress and the filenames and the 2nd loop gives the contents (directories) of the files.

awk 'NR%3==1{a[++i]=$0}END{for(f in a){print a[f], a[f] "_folderlist.txt"}}' unixfile.txt | 
while read ipaddress flist
do
  while read dir
  do
    # log in with $ipaddress
    # copy $dir .....
  done < $flist
done

Hope this helps.

Thanks a bunch Franklin, You're a genious! That's exactly what I need. I'm now trying to establish an FTP connection by using $ipaddress and transfer the $dir 's.
I'll let you know when I'm that far

Regards,
Laurens

EDIT: one more thing, how can I get all .txt files at once.

ftp -n -v immserv1.xx.xx.be << EOT
ascii
user U/N P/W
prompt
cd BACKUP
###get *.txt###
ls
EOT

Something like get *.txt , but that ain't working. Thx

Try it with mget.

Regards