是否有可能使一个bash shell脚本与其他命令行程序进行交互? [英] Is it possible to make a bash shell script interact with another command line program?

查看:339
本文介绍了是否有可能使一个bash shell脚本与其他命令行程序进行交互?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Linux终端使用交互式命令行程序运行bash shell的。我命令一个明确的顺序,我输入shell程序。程序将其输出写到标准输出。这些命令之一是保存命令,写入被运行previous命令的输出,一个文件到磁盘。

I am using a interactive command line program in a Linux terminal running the bash shell. I have a definite sequence of command that I input to the shell program. The program writes its output to standard output. One of these commands is a 'save' command, that writes the output of the previous command that was run, to a file to disk.

典型的循环是:

$prog
$$cmdx
$$<some output>
$$save <filename>
$$cmdy
$$<again, some output>
$$save <filename>
$$q
$<back to bash shell>


  • $ 是bash提示符

  • $ 是程序的提示

  • 问:的是quit命令的 PROG

  • PROG 的是这样的,它追加previous命令的输出的文件名

    • $ is the bash prompt
    • $$ is the program's prompt
    • q is the quit command for prog
    • prog is such that it appends the output of the previous command to filename
    • 我怎么能自动完成这一过程?我想通过的步骤写一个shell脚本,它可以启动该程序,并循环,喂养它的命令一招一式,然后退出。我希望的保存的命令可以正常工作。

      How can I automate this process? I would like to write a shell script that can start this program, and cycle through the steps, feeding it the commands one by one and, and then quitting. I hope the save command works correctly.

      推荐答案

      如果您的命令不在乎你给的速度有多快它的输入,你不真正需要的交互的它,那么你可以使用一个定界符。

      If your command doesn't care how fast you give it input, and you don't really need to interact with it, then you can use a heredoc.

      例如:

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

      如果您需要根据程序的输出分支,或者如果你的程序是在给你的命令的时间所有的敏感,然后期望是你想要的。

      If you need branching based on the output of the program, or if your program is at all sensitive to the timing of your commands, then Expect is what you want.

      这篇关于是否有可能使一个bash shell脚本与其他命令行程序进行交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆