How to insert a delay between pipelining commands in a bash script. E.g. cat file | telnet mail.domain.com 25
Asked Answered
S

2

18

I have a bash script that creates a file and I would like to send an email at the end via telnet. However sometimes it will execute and sometimes it won't.

The command at the end is

cat tempfile | telnet mail.domain.com 25

At the receiving server I see in mail.log the following error when it fails:

improper command pipelining after EHLO from domain.com ....etc

The same script works perfectly if instead of mail.domain.com I start the telnet session in localhost so I'm pretty sure the file format is OK and the rest of the bash script is working too.

I've also tried using standard redirection instead of a pipe

telnet mail.domain.com 25 < tempfile

But again the result sometimes is okay sometimes is not. I think there needs to be a small delay between the redirection and the telnet session command so that the input will be given after the telnet session has been established and a response has been given but I don't know how to do that. I've tried using sleep command in between pipes and redirection and it won't work probably because then the input is redirected to the sleep command.

e.g. cat tempfile | telnet mail.domain.com 25 & sleep 1

The restriction is that I have to do it in a bash script. Is it possible? Also I don't know if it's of any importance but the script used to work between servers in debian squeeze with postfix/courier setup and now the receiving end is set up with debian wheezy and postfix/dovecot.

Thanks in advance for the help

Shantae answered 11/10, 2013 at 18:59 Comment(0)
P
29

You could try something like:

(echo -n; sleep 5; cat tempfile) | mail.domain.com 25

to open the connection and write nothing, wait for 5 seconds and write the rest.

Popup answered 11/10, 2013 at 19:10 Comment(2)
{ echo -n; sleep 5; cat tempfile; } | ... would avoid an unnecessary subshell.Meredi
Great thanks for the fast response :) This worked flawlessly. Even without echo -n it will still wait for 5 seconds and then write the rest. I never thought about including it in (). I tried it with {} and it still works so both answers are good. However with {} i have to use echo -n;Shantae
S
4

How to insert a delay between pipelining commands in a bash script

If you want a delay for each piped line then use:

cat tempfile | { while read l ; do sleep 1; echo $l; done } | awk '{print $2}'

Lets say tempfile content is the following:

line1 content1
line2 content2
line3 content3

The above command will print:

line1
line2
line3

with each line being printed every second.

You could also add the { while read l ; do sleep 1; echo $l; done } inside a script file called delay-piped-line so that you use it like this:

cat tempfile | delay-piped-line | awk '{print $2}'
Snide answered 8/1, 2022 at 2:5 Comment(1)
Note that this is susceptible to backpressure, e.g., if lines come in faster than they go out.Throughout

© 2022 - 2024 — McMap. All rights reserved.