Solved

Combining here documents, and tee command

Posted on 2003-10-23
5
505 Views
Last Modified: 2007-12-19
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)

0
Comment
Question by:oumer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 5

Expert Comment

by:mtmike
ID: 9606846
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
 
LVL 4

Author Comment

by:oumer
ID: 9607047
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
 
LVL 5

Expert Comment

by:mtmike
ID: 9607106
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
 
LVL 4

Author Comment

by:oumer
ID: 9607170
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
 
LVL 5

Accepted Solution

by:
mtmike earned 30 total points
ID: 9607786
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do I delete this file in Linux 2 461
OS & DB Recommendation 4 53
Ubuntu 16.04 Emergency Boot 2 245
awk to variable in bash 2 151
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question