2024-07-03 13:09:47 +10:00
|
|
|
#!/usr/bin/python
|
2024-07-03 12:08:56 +10:00
|
|
|
import os
|
|
|
|
import subprocess
|
2024-07-03 12:38:20 +10:00
|
|
|
import re
|
2024-07-03 12:08:56 +10:00
|
|
|
|
|
|
|
initDirectory = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
currentDirectory = initDirectory
|
|
|
|
version = "0.0.1"
|
2024-07-03 13:31:27 +10:00
|
|
|
user = os.getlogin()
|
|
|
|
|
|
|
|
print("Welcome to Mash, " + user + "! Mash is currently on version " + version)
|
2024-07-03 12:08:56 +10:00
|
|
|
|
2024-07-03 12:38:20 +10:00
|
|
|
def grabArgument(fullCommand, commandRan):
|
|
|
|
fullCommand.replace(commandRan, "")
|
2024-07-03 13:09:47 +10:00
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
command = input(currentDirectory + " > ")
|
2024-07-03 14:46:40 +10:00
|
|
|
if command == "help":
|
|
|
|
print("This is Mash, Max's Shell. Mash has a few built in commands, which you can use to run commands, view files and interact with your system.")
|
|
|
|
print("Some commands you can run:")
|
|
|
|
print("")
|
|
|
|
print("pwd Print directory you are currently in")
|
|
|
|
print("cd Change your directory.")
|
|
|
|
print("cat View contents of a file.")
|
|
|
|
print("version Print current version of Mash.")
|
|
|
|
print("exit Exit Mash.")
|
|
|
|
print("")
|
|
|
|
print("You can also run commands installed by your system. See your OS's documentation for more information.")
|
|
|
|
elif command == "pwd":
|
2024-07-03 13:09:47 +10:00
|
|
|
print(currentDirectory)
|
2024-07-03 14:46:40 +10:00
|
|
|
elif command == "exit":
|
2024-07-03 13:09:47 +10:00
|
|
|
exit()
|
2024-07-03 14:46:40 +10:00
|
|
|
elif command == "cd":
|
2024-07-03 14:35:50 +10:00
|
|
|
try:
|
|
|
|
cdDirectory = input("Directory: ")
|
|
|
|
if cdDirectory[0] == "/":
|
|
|
|
if os.path.isdir(cdDirectory) == False:
|
|
|
|
if os.path.isfile(cdDirectory) == True:
|
|
|
|
print("That's not a directory, that's a file!")
|
|
|
|
else:
|
|
|
|
print("That directory doesn't exist.")
|
|
|
|
else:
|
|
|
|
currentDirectory = cdDirectory
|
|
|
|
else:
|
|
|
|
if os.path.isdir(currentDirectory + cdDirectory) == False:
|
|
|
|
if os.path.isfile(currentDirectory + cdDirectory) == True:
|
|
|
|
print("That's not a directory, that's a file!")
|
|
|
|
else:
|
|
|
|
print("That directory doesn't exist.")
|
|
|
|
else:
|
|
|
|
currentDirectory = cdDirectory
|
|
|
|
except Exception:
|
|
|
|
print("Either there was an error or that file doesn't exist.")
|
2024-07-03 13:09:47 +10:00
|
|
|
elif command == ("cat"):
|
|
|
|
try:
|
|
|
|
catFile = input("File to catalog: ")
|
|
|
|
catFileOpened = open(catFile)
|
|
|
|
print(catFileOpened.read())
|
|
|
|
except Exception:
|
|
|
|
print("File", catFile, "either doesn't exist or isn't in this directory.")
|
|
|
|
elif command == "version":
|
|
|
|
print(version)
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
subprocess.run(command)
|
|
|
|
except Exception:
|
|
|
|
print("Command", command, "either doesn't exist, isn't in the path or isn't in this directory.")
|
|
|
|
except Exception:
|
|
|
|
print("Looks like there was an error. Exiting...")
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print(" detected! Exiting...")
|