Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 07-14-2012, 07:51 AM   #1
Senior Member
Registered: Apr 2007
Location: bbsr,orissa,India
Posts: 1,378

Rep: Reputation: 135Reputation: 135
mput multiple data using for loop

I have ftp script to upload files after reading the contents of a file using for loop.

But the mail problem is that it just read the 1st line of the file and upload the related files and ftp exited.

My script is as below:
function ftptodenis {
#cd $DIR/data
for i in `cat $DIR/ERC_Track_File3`
ftp -n $HOST << EOF
quote USER $USER
quote PASS $PASS
prompt off
cd xxxxsap_ERC/data
mput $DIR/$i*.dat

It is not repeating through loop.
Old 07-14-2012, 08:22 AM   #2
Registered: Jun 2012
Location: Italy
Distribution: Slackware 13.37
Posts: 45

Rep: Reputation: Disabled
because in the loop you have multiple ftp connections

Last edited by silendo; 07-14-2012 at 08:24 AM.
Old 07-14-2012, 08:30 AM   #3
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2036Reputation: 2036Reputation: 2036Reputation: 2036Reputation: 2036Reputation: 2036Reputation: 2036Reputation: 2036Reputation: 2036Reputation: 2036Reputation: 2036
Well to start with, you need to learn how to format scripts so that they are more easily readable and debuggable. Indent your sub-commands and separate blocks of code with blank lines. It usually also helps to put the "then" and "do" keywords on the same line as the initial if/for/while/until keywords, to more clearly differentiate the outer code from the inner code.

For the here document, use the "<<-" form, which allows you to indent the lines with tabs. These will be removed before the contents are read.

The only major scripting mistake I see is here:

for i in `cat $DIR/ERC_Track_File3`
First of all, $(..) is highly recommended over `..`, but that's not the actual error. The error is:

Don't Read Lines With For!!

I don't know what the file contents look like, but if the lines contain whitespace or globbing characters, then you're going to get broken input. Use a while+read loop or some other form of input reading that can handle the data more safely, such as using a mapfile command to load the file into an array.

You probably also shouldn't hard-code the filename into your script. Set it up in a variable at the top of the script, or through some sort of external input, so that it can be more easily altered without having to hunt through the code.
1 members found this post helpful.
Old 07-14-2012, 10:09 AM   #4
Senior Member
Registered: Apr 2007
Location: bbsr,orissa,India
Posts: 1,378

Original Poster
Rep: Reputation: 135Reputation: 135
Thanks David for your suggestion.

These 2 things solved my problem.

1 - Using $(..)

2 - mput/mget should take the relative path of file as Full path/Variable stored path gives the error :A file or directory in the path name does not exist.

Last edited by divyashree; 07-14-2012 at 10:12 AM.


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
[SOLVED] Populate multiple variables via loop theillien Programming 12 05-27-2012 10:00 AM
multiple conditions in while() loop in C glosterj9 Programming 1 12-27-2011 12:16 PM
Multiple variable for loop (bash) Phier Programming 9 11-24-2009 09:54 AM
Multiple variable for loop (bash) Phier Linux - Newbie 2 11-23-2009 12:13 PM
Comparing Multiple Values in while loop rahulruns Programming 5 10-27-2009 06:59 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:54 PM.

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