From da125b103303d1e39b2c281d7995c29a50268dc3 Mon Sep 17 00:00:00 2001 From: JaydenMW Date: Sun, 14 Feb 2021 03:34:00 +0100 Subject: [PATCH] Update 'shell.py' --- shell.py | 235 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 120 insertions(+), 115 deletions(-) diff --git a/shell.py b/shell.py index 64491d8..0f2b563 100644 --- a/shell.py +++ b/shell.py @@ -1,115 +1,120 @@ -"""pclish: a simple shell written in Python""" - -import os -import subprocess -import psutil -import platform - -# Get System Information -RAM = psutil.virtual_memory() -CPU = platform.processor() - -# Command Interpretation -- This is the core of the shell -def execute_command(command): - """execute commands and handle piping""" - try: - if "|" in command: - s_in, s_out = (0, 0) - s_in = os.dup(0) - s_out = os.dup(1) - - fdin = os.dup(s_in) - - for cmd in command.split("|"): - os.dup2(fdin, 0) - os.close(fdin) - - if cmd == command.split("|")[-1]: - fdout = os.dup(s_out) - else: - fdin, fdout = os.pipe() - - os.dup2(fdout, 1) - os.close(fdout) - - try: - subprocess.run(cmd.strip().split()) - except Exception: - print("pclish: command not found: {}".format(cmd.strip())) - - os.dup2(s_in, 0) - os.dup2(s_out, 1) - os.close(s_in) - os.close(s_out) - else: - subprocess.run(command.split(" ")) - except Exception: - print("pclish: command not found: {}".format(command)) - - -# Commands - Commands are defined here. -def pclish_cd(path): - """convert to absolute path and change directory""" - try: - os.chdir(os.path.abspath(path)) - except Exception: - print("cd: no such file or directory: {}".format(path)) - - -def pclish_help(): - print("""pclish: here are the commands available - help: shows this page - cd: change directory - ver: displays shell version - ls: lists files in current dir""") - -def pclish_ls(): - print("""pclish: this feature is not yet supported""") - -def pclish_ver(): - print("""pclish: version 0.0.1a.""") - -def pclish_mkdir(): - value = input("What would you like to name this new directory:\n") - dir = value - os.mkdir(dir) - -def pclish_system(): - print("""System: XCU Python Based Shell Env""") - print("RAM:") - print(RAM) - print("CPU:") - print(CPU) - - -def pclish_shtdwnsubsys(): - print("Shutting Down XCU Python Based Shell Env") - exit() - -# Shell Prompt Customization and step 2 of command interpritation -def main(): - while True: - inp = input("shell@system >> ") - if inp == "exit": - break - elif inp[:3] == "cd ": - pclish_cd(inp[3:]) - elif inp == "help": - pclish_help() - elif inp == "ver": - pclish_ver() - elif inp == "ls": - pclish_ls() - elif inp == "mkdir": - pclish_mkdir() - elif inp == "shtdwnsubsys": - pclish_shtdwnsubsys() - elif inp == "system": - pclish_system() - else: - execute_command(inp) - - -# Main -if '__main__' == __name__: - main() +"""pclish: a simple shell written in Python""" + +import os +import subprocess +import psutil +import platform + +# Get System Information +RAM = psutil.virtual_memory() +CPU = platform.processor() + +# Command Interpretation -- This is the core of the shell +def execute_command(command): + """execute commands and handle piping""" + try: + if "|" in command: + s_in, s_out = (0, 0) + s_in = os.dup(0) + s_out = os.dup(1) + + fdin = os.dup(s_in) + + for cmd in command.split("|"): + os.dup2(fdin, 0) + os.close(fdin) + + if cmd == command.split("|")[-1]: + fdout = os.dup(s_out) + else: + fdin, fdout = os.pipe() + + os.dup2(fdout, 1) + os.close(fdout) + + try: + subprocess.run(cmd.strip().split()) + except Exception: + print("pclish: command not found: {}".format(cmd.strip())) + + os.dup2(s_in, 0) + os.dup2(s_out, 1) + os.close(s_in) + os.close(s_out) + else: + subprocess.run(command.split(" ")) + except Exception: + print("pclish: command not found: {}".format(command)) + + +# Commands - Commands are defined here. +def pclish_cd(path): + """convert to absolute path and change directory""" + try: + os.chdir(os.path.abspath(path)) + except Exception: + print("cd: no such file or directory: {}".format(path)) + +def pclish_echo + txt = input("What would you like to echo?:\n") + echo(txt) + +def pclish_help(): + print("""pclish: here are the commands available + help: shows this page + cd: change directory + ver: displays shell version + ls: lists files in current dir""") + +def pclish_ls(): + print("""pclish: this feature is not yet supported""") + +def pclish_ver(): + print("""pclish: version 0.0.1a.""") + +def pclish_mkdir(): + value = input("What would you like to name this new directory:\n") + dir = value + os.mkdir(dir) + +def pclish_system(): + print("""System: XCU Python Based Shell Env""") + print("RAM:") + print(RAM) + print("CPU:") + print(CPU) + + +def pclish_shtdwnsubsys(): + print("Shutting Down XCU Python Based Shell Env") + exit() + +# Shell Prompt Customization and step 2 of command interpritation +def main(): + while True: + inp = input("shell@system >> ") + if inp == "exit": + break + elif inp[:3] == "cd ": + pclish_cd(inp[3:]) + elif inp == "help": + pclish_help() + elif inp == "ver": + pclish_ver() + elif inp == "ls": + pclish_ls() + elif inp == "mkdir": + pclish_mkdir() + elif inp == "shtdwnsubsys": + pclish_shtdwnsubsys() + elif inp == "system": + pclish_system() + elif inp = "echo": + pclish_echo() + else: + execute_command(inp) + + +# Main +if '__main__' == __name__: + main()