Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Combining here documents, and tee command

Posted on 2003-10-23
5
Medium Priority
?
509 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 120 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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 fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Screencast - Getting to Know the Pipeline

610 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