是否可以使bash shell脚本与另一个命令行程序交互?

时间:2010-08-10 11:35:51

标签: linux bash shell

我在运行bash shell的Linux终端中使用交互式命令行程序。我有一个确定的命令序列,我输入到shell程序。程序将其输出写入标准输出。其中一个命令是'save'命令,它将上一个运行的命令的输出写入文件到磁盘。

典型的周期是:

$prog
$$cmdx
$$<some output>
$$save <filename>
$$cmdy
$$<again, some output>
$$save <filename>
$$q
$<back to bash shell>
  • $ 是bash提示
  • $$ 是程序的提示
  • q prog
  • 的退出命令
  • prog 是这样的,它将上一个命令的输出附加到 filename

如何自动完成此过程?我想写一个shell脚本,可以启动这个程序,循环执行这些步骤,逐个输入命令,然后退出。我希望 save 命令正常工作。

6 个答案:

答案 0 :(得分:15)

如果你的命令不关心你输入的速度有多快,而你真的不需要与它进行交互,那么你可以使用heredoc。

示例:

#!/bin/bash
prog <<EOD
cmdx
save filex
cmdy
save filey
q
EOD

如果您需要根据程序的输出进行分支,或者您的程序对命令的时间敏感,那么Expect就是您想要的。

答案 1 :(得分:11)

我建议您使用Expect。此工具旨在自动化交互式shell应用程序。

答案 2 :(得分:6)

有需要的地方,有办法!我认为这是一个很好的打击课 流程管理和ipc如何工作。当然,最好的解决方案是Expect。 但真正的原因是管道可能很棘手并且设计了许多命令 等待数据,这意味着该过程将成为一个僵尸的原因 海湾很难预测。但是学习如何以及为什么提醒我们什么是 在引擎盖下进行。

当两个进程进行对话时,危险就是一个或两个进程 尝试读取永远不会到达的数据。参与规则必须是 晶莹剔透。像CRLF和字符编码之类的东西可以杀死派对。 幸运的是,两个密切的合作伙伴,如bash脚本及其子进程 相对容易保持一致。最容易错过的是bash 为它所做的每件事启动一个子进程。如果你能成功的话 与bash一起工作,你完全知道自己在做什么。

关键是我们想要与另一个流程进行对话。这是服务器:

# a really bad SMTP server

# a hint at courtesy to the client
shopt -s nocasematch

echo "220 $HOSTNAME SMTP [$$]"

while true
do
    read
    [[ "$REPLY" =~ ^helo\ [^\ ] ]] && break
    [[ "$REPLY" =~ ^quit ]] && echo "Later" && exit
    echo 503 5.5.1 Nice guys say hello.
done

NAME=`echo "$REPLY" | sed -r -e 's/^helo //i'`
echo 250 Hello there, $NAME 

while read
do
    [[ "$REPLY" =~ ^mail\ from: ]] && { echo 250 2.1.0 Good guess...; continue; }
    [[ "$REPLY" =~ ^rcpt\ to: ]] && { echo 250 2.1.0 Keep trying...; continue; }
    [[ "$REPLY" =~ ^quit ]] && { echo Later, $NAME; exit; }
    echo 502 5.5.2 Please just QUIT
done

echo Pipe closed, exiting

现在,希望能够发挥魔力的剧本。

# Talk to a subprocess using named pipes

rm -fr A B      # don't use old pipes
mkfifo A B

# server will listen to A and send to B
./smtp.sh < A > B &

# If we write to A, the pipe will be closed.
# That doesn't happen when writing to a file handle.
exec 3>A

read < B
echo "$REPLY"

# send an email, so long as response codes look good
while read L
do
    echo "> $L"
    echo $L > A
    read < B
    echo $REPLY
    [[ "$REPLY" =~ ^2 ]] || break

done <<EOF
HELO me
MAIL FROM: me
RCPT TO: you
DATA
Subject: Nothing

Message
.
EOF

# This is tricky, and the reason sane people use Expect.  If we
# send QUIT and then wait on B (ie. cat B) we may have trouble.
# If the server exits, the "Later" response in the pipe might
# disappear, leaving the cat command (and us) waiting for data.
# So, let cat have our STDOUT and move on.
cat B &

# Now, we should wait for the cat process to get going before we
# send the QUIT command. If we don't, the server will exit, the
# pipe will empty and cat will miss its chance to show the
# server's final words.
echo -n > B     # also, 'sleep 1' will probably work.

echo "> quit"
echo "quit" > A

# close the file handle
exec 3>&-

rm A B

请注意,我们不是简单地在服务器上转储SMTP命令。我们检查 每个响应代码,以确保一切正常。在这种情况下,事情不会 好的,脚本将保释。

答案 3 :(得分:2)

我使用Expect与shell交互以进行交换机和路由器备份。 bash脚本使用正确的变量调用expect脚本。

for i in; do expect_script.sh $ i;出口

这将ssh到每个框,运行备份命令,复制出适当的文件,然后转到下一个框。

答案 4 :(得分:1)

对于简单的用例,您可以使用子shell,echo和amp;组合。睡眠:

# in Terminal.app
telnet localhost 25
helo localhost
ehlo localhost
quit

(sleep 5; echo "helo localhost"; sleep 5; echo "ehlo localhost"; sleep 5; echo quit ) | 
   telnet localhost 25 

答案 5 :(得分:0)

echo "cmdx\nsave\n...etc..." | prog

...

相关问题