I am attempting to create a basic terminal emulator.
Currently my code takes input, and pastes it straight into Subprocess.run.
import subprocess
while True:
command = input("Input:")
result = subprocess.run(
command.split(),
capture_output=True,
encoding="utf-8")
result.check_returncode()
result = result.stdout
print(result)
I would like to be able to run a series of commands in the same prompt.
For example, I would like to input
cd /Desktop
ls
and then
cat myfile.txt
However, "cd" returns an error:
> [Errno 2] No such file or directory: 'cd'
Presumably, this is because subprocess.run creates a new subprocess for each command, so there is no point in such a command being supported.
Similarly, the problem rearises when inputting a command that requires sudo, eg
> sudo mount /dev/sda1 /home/sda1
I am therefore wondering how subprocess (or a different library) can be used to run a series of processes in the same terminal in series, simply pasting plain text into a terminal and recieving the output back as plain text, with the ability to send commands to the same subprocess, rather then a new spawned subprocess.
(I am aware that such a utility creates a severe security issue, as it gives a program full access to the terminal)