2

I would like to simulate a coding session (for video recording session : I am not a touch typist :-)

For example, I have a shell script like this (test.sh)

hello="Hello"
world="world"
echo $hello", "$world

And I have a python script like this (Simulate_KeyPresses.py) :

import sys
import time
import subprocess

def send_letter(letter):
  # V1 : simple print
  sys.stdout.write(letter)
  sys.stdout.flush()

  # V2: Test with expect (apt-get install expect)
  # cmd = """echo 'send "{}"' | expect""".format(c)
  # subprocess.run(cmd, shell=True)


def simulate_keypresses(content):
  lines = content.split("\n")
  for line in lines:
    for c in line:
      send_letter(c)
      time.sleep(0.03)
    send_letter("\n")
    time.sleep(0.5)


if __name__ == "__main__":
  filename = sys.argv[1]
  with open(filename, "r") as f:
    content = f.read()
  simulate_keypresses(content)

Which I can invoke like this :

python Simulate_KeyPresses.py test.sh

And it works beautifully. However when I pipe it to bash, like this:

python Simulate_KeyPresses.py test.sh | /bin/bash

I get

Hello, world

i.e I only get stdout and the key presses are not shown.

What I would like to see:

hello="Hello"
world="world"
echo $hello", "$world
Hello, world

I found a related answer (Simulate interactive python session), but it only handle python coding sessions.

I tried to use Expect, but it does not work as intended (does not show stdin also).

Help would be appreciated!

2 Answers 2

3

You can use the program tee as:

python Simulate_KeyPresses.py test.sh | tee /dev/tty | /bin/bash
Sign up to request clarification or add additional context in comments.

Comments

0

How about adding this to your script:

subprocess.call("./{}".format(filename), shell=True)

The result will be

hello="Hello"
world="world"
echo $hello", "$world

Hello, world

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.