Combining here documents, and tee command

The question is
how can I make the tee command work from "here documents"

What I wanted to do is to do some bash code which does like this
ftp to a server,
download a certain file a certain number of times, so that I can get the average throughput)
put the output from FTP (so that I can parse the lines that tells me about the throughput and do something)
and quit. Things work perfectly if I don't use the tee command as shown at the bottom, but without the tee command,

#parameter 1 is the file name that I want to copy
#parameter 2 is the number of times I want to get the file

filename=$1
count=$2
newline="\n"
basic_command="get $filename"
command=$basic_command

for i in $(seq 2 $count)
do
command=$command$newline$basic_command
done

ftp server | tee ftp.out <<EOF   #I have .netrc file, so I need no login, and here is where things go wrong
$(echo -e $command)
quit
<<EOF


but the tee seems not to work, and I get the error "No Control  connection for command: Broken pipe" and "No control connection for command: Illegal seek"
so how can I make the tee work?
Also, instead of just get file_name, I wanted to put another command say, get the file n times, so I did something like this


I also thought of first making the commands into a script file and make a macdef out of it in my .netrc file, but as I described in the question that I have asked in the coulumn before (and unfortunatley, no answer found http://www.experts-exchange.com/Programming/Programming_Platforms/Linux_Programming/Q_20742506.html), my macdefs with .netrc don't work. and as far as I could see, there is no option in the linux ftp command (from the man pages), to give a command list to it. (like the s:scriptfile option in window)

LVL 4
oumerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mtmikeCommented:
I think the here document needs to placed before the '|'. This should work:

ftp -v server << EOF
$(echo -e $command)
quit
EOF | tee ftp.out

I also tried using macdefs in the .netrc file. This worked fine for me, as long as each macdef includes a trailing blank line.

Another option is to use a non-interactive transfer tool such as wget. It outputs the throughput too.
See http://www.gnu.org/software/wget/wget.html
0
oumerAuthor Commented:
I have also tried to put the tee at the end as you suggested, but this time the command runs but the file ftp.out is not created
0
mtmikeCommented:
Did you use the -v switch? Otherwise, ftp remains silent (it detects that its input is non-interactive) and the output file is not created.
0
oumerAuthor Commented:
THat helps a little.
The tee still doesn't work (I only see the result in stdio.h)
so I can remove the tee command from inside the script and call like
myscript | tee   ftp.out (this works)
or
ftp -v server > ftp.out << EOF
$(echo -e $command)
quit
EOF

but why does the tee doesn't inside the script, I don't know
0
mtmikeCommented:
This is getting tricky...

First option:

ftp server | tee ftp.out << EOF
$(echo -e $command)
quit
EOF

But this isn't right since the '| tee ftp.out' must be at the end of the line. Othwerwise, the here document becomes the stdin of tee and the ftp command gets a broken pipe since tee can only have one stdin.

Second option:

ftp server << EOF
$(echo -e $command)
quit
EOF | tee ftp.out

This seem to work in the shell, but not in a a script because the end delimiter of the here document must be on a single line without trailing whitespace.

Seems to me that you can't use here documents and a pipeline at the same time. You can of course use a simpler solution without the here document:

filename=$1
count=$2
newline="\n"
quit="quit"
basic_command="get $filename"
command=

for i in $(seq 1 $count)
do
command=$command$basic_command$newline
done
command=$command$quit

echo -e $command | ftp -v server | tee ftp.out
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux OS Dev

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.