Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 511
  • Last Modified:

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)

0
oumer
Asked:
oumer
  • 3
  • 2
1 Solution
 
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now