Solved

Combining here documents, and tee command

Posted on 2003-10-23
5
499 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now