How to implement stdin shell, stdout?

I have an interactive program that runs stdin and stdout. I need to create a wrapper that will send X stdin to it, make sure it prints Y, and then redirect the wrapper stdin and stdout to the stdin and stdout program just like the program will execute directly.

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

Edit: I cannot run the program twice. This should be one case. Here is the pseudo code:

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() 
+3
source share
4 answers

Expect - , -

. " ", . "", .

.

+3

, X Y , :

#!/bin/bash

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

, :

#!/bin/bash

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

program

, Expect , - .

+1

sys- stdin stdout

import sys
sys.stdin, sys.stdout = wrapper.stdin, wrapper.stdout

, . stdin stdout

sys.stdin, sys.stdout = sys.__stdin__, sys.__stdout__
0

, ; , :

  • - X Y.
  • , X stdin, , Y.
  • -, - stdin stdout.

, :

  • - stdin stdout .
  • .
  • , exec() .
  • , , .
  • - exec() .
  • .

If this is correct, I can provide a 30-line C program or a 10-line Python program that achieves this.

0
source

Source: https://habr.com/ru/post/1725444/


All Articles