Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to make a bash shell script interact with another command line program?

Tags:

linux

bash

shell

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.

A typical cycle is:

$prog
$$cmdx
$$<some output>
$$save <filename>
$$cmdy
$$<again, some output>
$$save <filename>
$$q
$<back to bash shell>
  • $ 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

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.

like image 885
rup Avatar asked Aug 10 '10 11:08

rup


People also ask

Can CMD use bash?

To access the shell, simply type 'bash' in the Windows command prompt, and everything is good to go.


2 Answers

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.

Example:

#!/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.

like image 168
Tristan Avatar answered Sep 19 '22 14:09

Tristan


I recommend you use Expect. This tool is designed to automate interactive shell applications.

like image 41
schot Avatar answered Sep 19 '22 14:09

schot