-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.py
executable file
·51 lines (35 loc) · 1.23 KB
/
shell.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
import subprocess
import os
def execute_command(command_input):
command_input_args = command_input.split(" ")
try:
# Open a subprocess with the command
process = subprocess.Popen(command_input_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
# Communicate with the subprocess (send input, receive output)
stdout, stderr = process.communicate()
print(stdout)
if process.returncode != 0:
print("Error:", stderr)
except Exception as e:
print("Unexpected error:", e)
def startup_shell():
while True:
command_input = input("cgsh> ").strip()
if command_input == "exit":
print("Exiting shell...")
break
# split with | for bigger commands
# handle cd differently
if "cd" in command_input:
input_directory = command_input[3:]
try:
os.chdir(input_directory)
except FileNotFoundError:
print(f"Directory not found: {input_directory}")
# handle other commands differently
else:
execute_command(command_input)
return
if __name__ == "__main__":
startup_shell()