Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a stdin, stdout wrapper?

I have an interactive program that runs stdin and stdout. I need to create wrapper that will send X to it's stdin, check that it prints Y and then redirects wrapper's stdin and stdout to program's stdin and stdout just like program would be executed directly.

How to implement this ? X and Y can be hardcoded. Bash? Python?

Edit: I can't run the program twice. It has to be one instance. Here is the pseudocode:

def wrap(cmd, in, expected_out):
  p = exec(cmd)
  p.writeToStdin(in)
  out = p.readBytes (expected_out.size())
  if (out != expected_out) return fail;
  # if the above 4 lines would be absent or (in == "" and out == "")
  # then this wrapper would be exactly like direct execution of cmd
  connectpipe (p.stdout, stdout)
  connectpipe (stdin, p.stdin)
  p.continueExecution() 
like image 750
Łukasz Lew Avatar asked Oct 15 '22 11:10

Łukasz Lew


2 Answers

Expect is made for automating the running of other programs - essentially you write something like, in plain text,

Start this program. When it prints out the word "username", send it my username. When it sends "password", send it my password.

It's really great for driving other programs.

like image 194
Matt Avatar answered Oct 21 '22 03:10

Matt


Assuming X and Y are files, and that you can invoke the program more than once:

#!/bin/bash

test "`program <X`" = "`cat Y`" && program

Or, to fail more verbosely:

#!/bin/bash

if [[ `program <X` != `cat Y` ]]; then
    echo -e "Assertion that input X produces Y failed, exiting."
    exit 1
fi

program

If you only invoke the program once, Expect is a much simpler alternative than reassigning standard file I/O on the fly.

like image 26
system PAUSE Avatar answered Oct 21 '22 03:10

system PAUSE