如何在 bash 脚本中的流水线命令之间插入延迟。例如 cat 文件 | telnet mail.domain.com 25

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19325355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:16:19  来源:igfitidea点击:

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

linuxbashemailtelnet

提问by gabtzi

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.

我有一个创建文件的 bash 脚本,我想在最后通过 telnet 发送一封电子邮件。但是,有时它会执行,有时则不会。

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:

在接收服务器上,我在 mail.log 中看到失败时出现以下错误:

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.

如果我在 localhost 中启动 telnet 会话而不是 mail.domain.com,则相同的脚本可以完美运行,因此我很确定文件格式没问题,并且 bash 脚本的其余部分也可以正常工作。

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.

但同样的结果有时是好的有时不是。我认为重定向和 telnet 会话命令之间需要有一个小的延迟,以便在 telnet 会话建立并给出响应后给出输入,但我不知道如何做到这一点。我试过在管道和重定向之间使用 sleep 命令,它可能无法工作,因为输入被重定向到 sleep 命令。

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.

限制是我必须在 bash 脚本中执行此操作。是否可以?此外,我不知道它是否有任何重要性,但该脚本曾经在 debian 压缩中的服务器之间使用 postfix/courier 设置工作,现在接收端设置为 debian wheezy 和 postfix/dovecot。

Thanks in advance for the help

在此先感谢您的帮助

回答by Paul Nechifor

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.

打开连接,什么都不写,等待 5 秒,然后写剩下的。