【问题标题】:Piping to a command in Ash Shell管道到 Ash Shell 中的命令
【发布时间】:2013-07-07 18:53:58
【问题描述】:

我编写了一个 bash 脚本来使用 telnet 发送电子邮件。我将它安装在运行busyBox(有一个灰壳)的TS-7260上。

Bash 和 Ash 之间有些不同,我无法弄清楚为什么以下内容不起作用。这与我将回声传输到 telnet 的方式有关。这是脚本:

#!/bin/ash

# Snag all the error messages from a given date, open a telnet connection to an outgoing mail server, stick the logs in an email, and send it.
# Tue Jul  2 14:06:12 EDT 2013
# TMB

# Tue Jul  9 17:12:29 EDT 2013
# Grepping the whole error file for WARNING and the piping it to a grep for the date took about four minutes to complete on the gateway.  This will only get longer and the file will only get bigger as time goes by.
# Using tail to get the last 5000 lines, I get about three days of errors (2000 of them are from one day, though)
# Getting 5000 lines, then searching them by WARNING and then DATE took 15 seconds on the gateway.

yesterdayDate=$(./getYesterday)

warningLogs=$(tail -5000 /mnt/sd/blah.txt | grep WARNING | grep "$yesterdayDate")

sleep 30

{
sleep 5
echo "ehlo blah.com"
sleep 5
echo "auth plain blah"
sleep 5
echo "mail from: blah@blah.com"
sleep 5
echo "rcpt to: me@blah.com"
sleep 5
echo "data"
sleep 5

echo "Hi!"
sleep 1
echo "Here are all the warnings and faults from yesterday:"
sleep 1
echo "$yesterdayDate"
sleep 1
echo "NOTE: All times are UTC."
sleep 1
echo ""
sleep 1
echo "$warningLogs"
sleep 10
echo ""
sleep 1
echo "Good luck,"
sleep 1
echo "The Robot"
sleep 5
echo "."
sleep 20
echo "quit"
sleep 5
} | telnet blah.com port

exit

我也尝试在管道之前使用普通括号。我已经阅读了 ash 的手册页,但仍在做一些愚蠢的事情。我怀疑这是在进行某种子流程业务。

这在 bash 中运行良好,顺便说一句。

提前致谢!


注意——我将脚本简化为:

echo "quit" | telnet blah.com port

它完全符合您在 bash 中的期望,但我发现在 ash 中什么也没有发生。 将 echo 替换为“sleep 10”表示 sleep 作为进程运行,而不是 telnet。

【问题讨论】:

  • 我假设任何 shell 都支持使用 set -vx 进行调试跟踪。尝试打开它,在一个窗口中在 bash 下运行,在另一个窗口中运行 ash。看看哪里有区别? ash 是否支持 $(... cmd-substitution) ?我想我读过它没有。使用 back-tics (yik) 代替?祝你好运!
  • 究竟是什么不起作用?不同的输出,显式错误退出,还有什么?
  • 看起来管道的工作方式似乎有所不同——“set -vx”显示了正在执行的命令,但是查看系统上运行的进程,telnet 从不运行(或者它运行太短的时间让我赶不上)。在 bash 上,确实如此,并且您会看到响应回显。据我所知,$() 在灰烬上效果很好。

标签: bash pipe telnet busybox ash


【解决方案1】:

经过更多的实验,问题根本不在于 shell,而在于 Busybox 上的 Telnet 实现。在我的 BusyBox (1.00rc2) 版本上,将任何内容通过管道传输到 Telnet 都不起作用。

echo blah | telnet -yrDumb

至少应该让 telnet 抱怨使用情况。它没有。 我获取了最新版本的 inetutils (1.9.1) 并为 TS-7260 编译了它的 telnet。它现在像梦一样工作(阅读:它工作),并且与我在普通 linux 机器上使用 telnet 和 bash 看到的行为一致。

感谢您的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多