LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-13-2006, 01:43 AM   #1
gn00kie
Member
 
Registered: Jan 2006
Posts: 70

Rep: Reputation: 15
Suggestion on coding this kind of requirement


Hi,
I would like to ask suggestion from you guys on how can I achieve this requirement. I would like to connect a a public ftp server, I already have the code to connect to it via script however, my requirement is to download only files that are listed on a txt file. I could do a command such as mget file1 file2 file3...etc. But how do I feed to the ftp program the list of file names that it should download. Here is the snippet of my code. TIA.

#!/usr/bin/bash

ftp -i -n << "END"
open publicserver.com
user anonymous 123
bin
cd loc1
mget file1.asc file2.asc
bye
END
echo "ftp complete"
 
Old 09-13-2006, 01:48 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,349

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
Add this cmd
$file_list=`cat file_list.txt`
before the ftp stuff

then amend the mget
mget $file_list
 
Old 09-13-2006, 02:00 AM   #3
gn00kie
Member
 
Registered: Jan 2006
Posts: 70

Original Poster
Rep: Reputation: 15
can i output an ls command such that it would fit the requirements of the mget of ftp? For example, ls /dir > file_list.txt, where in the output would be file1 file2 file3?
 
Old 09-13-2006, 09:20 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,349

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
If you just want the entire dir, use
mget *
or
mget *.*
 
Old 09-13-2006, 10:30 PM   #5
gn00kie
Member
 
Registered: Jan 2006
Posts: 70

Original Poster
Rep: Reputation: 15
nope, I am not going to get all the files, only the files that are included on the list.
 
Old 09-14-2006, 12:07 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,349

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
That's what my 1st suggestion was for ...
 
Old 09-14-2006, 12:33 AM   #7
gn00kie
Member
 
Registered: Jan 2006
Posts: 70

Original Poster
Rep: Reputation: 15
have already manually edited the file. thanks for your suggestion.
 
Old 09-14-2006, 11:59 AM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by gn00kie
can i output an ls command such that it would fit the requirements of the mget of ftp? For example, ls /dir > file_list.txt, where in the output would be file1 file2 file3?
If you meant to only download files with the same name as the files in /dir on your local machine, then the answer is yes.

You could also do $files_list=`ls /dir` which amounts to the same thing.
 
Old 09-14-2006, 12:33 PM   #9
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
Something like that maybe:
Code:
#!/usr/bin/bash

ftp -i -n << "END"
open publicserver.com
user anonymous 123
bin
cd loc1
mget $(cat /path/to/text/file.txt)
bye
END
echo "ftp complete"
And in case the filenames appear in the file one per line, and ftp does not like this, just replace the "cat ...txt" command with: "tr '\n' ' ' </path/to/text/file.txt"

Yves.
 
Old 09-18-2006, 12:49 AM   #10
gn00kie
Member
 
Registered: Jan 2006
Posts: 70

Original Poster
Rep: Reputation: 15
Hi,
When I try to run the script, I get a $file_list.txt: no such file or directory error. I think ftp can not translate the value of my cat command...
 
Old 09-18-2006, 01:56 AM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
That's where sometimes, the functionality of some commands doesn't let the programmer have control of.
Here's a simple in Perl too where there's more "control" ...

Code:
use Net::FTP;
...
...
$ftpobj = Net::FTP -> new ($server,Timeout=>20) or
        die "Cannot access $server via FTP\n";
$ftpobj -> login($server,$pwd);
$ftpobj -> cwd ("$dir");
foreach $files (@filearray) {
   $ftpobj -> get ($files);
}
$ftpobj -> quit;
 
Old 09-18-2006, 03:36 AM   #12
gn00kie
Member
 
Registered: Jan 2006
Posts: 70

Original Poster
Rep: Reputation: 15
Thanks for suggesting perl but is there another way for bash shell to do it?
 
Old 09-19-2006, 04:59 AM   #13
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
Quote:
Originally Posted by gn00kie
$file_list.txt: no such file
What exactly did you write?

ftp is not evaluating $(...) constructs, bash is. But you have to write it exactly so, with '$(' at the start of the command, and ')' at the end.

Yves.
 
Old 09-19-2006, 11:54 PM   #14
gn00kie
Member
 
Registered: Jan 2006
Posts: 70

Original Poster
Rep: Reputation: 15
this my code:

#!/usr/bin/bash

ftp -i -n << "END"
open www.publicserver.com
user anonymous 123
bin
cd public
mget $(cat /export/home/noc/vsftpd-2.0.4/bin/files1.txt)
bye
END
echo "ftp complete"

When I try to run it, I get this error: "$(cat: No such file or directory.
/export/home/noc/vsftpd-2.0.4/bin/files1.lst): No such file or directory.
can't find list of remote files, oops
ftp complete"

I agree that bash can translate variables with $ but on my script, ftp was invoked on bash and the way I see it, ftp is the one evaluating the variables enclosed on the << END portion of the program.

Last edited by gn00kie; 09-20-2006 at 12:33 AM.
 
Old 09-20-2006, 03:57 AM   #15
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
OK I get it.
It's because you put quotes around END. Try without the quotes, like this: ftp -i -n <<END

Yves.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
fedora requirement shadowsurfer Linux - Newbie 1 09-09-2004 07:48 PM
what is the requirement tony2666 Fedora - Installation 1 05-20-2004 02:58 PM
kind of a programming quesion...kind of not tho jhorvath Programming 2 06-30-2003 10:05 PM
Requirement for Beginners jcornel7 Programming 13 02-08-2003 09:37 AM
software requirement takako Linux - General 6 05-09-2002 08:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration