-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.py
More file actions
151 lines (130 loc) · 5.7 KB
/
shell.py
File metadata and controls
151 lines (130 loc) · 5.7 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
'''
MIT License
Copyright (c) 2024 Filip Nachov
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.'''
import os
import subprocess
# all shell commands
class Shell_commands:
def exit_cmd(self):
print("Ending shell session")
exit(0)
def echo_cmd(self, args):
print(" ".join(args))
def print_cmd(self, args):
print(" ".join(args))
def mkdir_cmd(self, name):
os.makedirs(name)
def ls_cmd(self, directory):
files = os.listdir(directory)
num = 0
for i in files:
num += 1
print(f"file {num}: {i}")
def pwd_cmd(self):
path = os.getcwd()
print(path)
def cd_cmd(self, path):
os.chdir(path)
def rmdir_cmd(self, name):
os.rmdir(name)
def touch_cmd(self, name):
open(name, "w")
def rm_cmd(self, name):
try:
os.remove(name)
print(f"File '{name}' deleted successfully.")
except OSError as e:
print(f"Error: {e}")
def help_cmd(self):
print("exit: exits the shell")
print("pwd: shows your current path")
print("cd: changes the current directory")
print("ls: shows all the items in the current directory")
print("echo or print <word/sentence>: prints out the word or sentences said")
print("mkdir <name>: creates a directory with that name")
print("rmdir <name>: deletes the directory registered under that name")
print("touch <name>: make a file with that name")
print("rm <name>: delets the file under that name")
# the shell working system
class Shell:
def __init__(self):
# defining all the commands from the Shell_commands class
self.commands = {} # making a hashmap
shell_commands = Shell_commands # connecting the class into a variable
self.commands["exit"] = shell_commands.exit_cmd # exit command
self.commands["echo"] = shell_commands.echo_cmd # echo command
self.commands["print"] = shell_commands.print_cmd # print command
self.commands["ls"] = shell_commands.ls_cmd # ls command
self.commands["pwd"] = shell_commands.pwd_cmd # pwd commnad
self.commands["cd"] = shell_commands.cd_cmd # cd command
self.commands["help"] = shell_commands.help_cmd # help command
self.commands["mkdir"] = shell_commands.mkdir_cmd # mkdir command
self.commands["rmdir"] = shell_commands.rmdir_cmd # rmdir command
self.commands["touch"] = shell_commands.touch_cmd # touch command
self.commands["rm"] = shell_commands.rm_cmd # rm command
def run(self):
print('For all available commands type "help"')
while True: # making a while loop for the input
path = os.getcwd() # taking path
inputing = input(f"$ {path}: ") # taking input
if inputing == "exit": # the if system to run the wanted command
self.commands["exit"](self)
elif inputing.startswith("echo"):
args = inputing.split()[1:]
self.commands["echo"](self, args)
elif inputing.startswith("print"):
args = inputing.split()[1:]
self.commands["print"](self, args)
elif inputing == "ls":
path = os.getcwd()
self.commands["ls"](self, path)
elif inputing == "pwd":
self.commands["pwd"](self)
elif inputing.startswith("cd"):
loi = inputing.split()
path = loi[1]
try:
self.commands["cd"](self, path)
except FileNotFoundError:
print(f"Error: Directory '{path}' not found")
except PermissionError:
print(f"Error: Permission denied to access directory '{path}'")
elif inputing == "help":
self.commands["help"](self)
elif inputing.startswith("mkdir"):
loi = inputing.split()[1:]
name = loi[0]
self.commands["mkdir"](self, name)
elif inputing.startswith("rmdir"):
loi = inputing.split()[1:]
name = loi[0]
try:
self.commands["rmdir"](self, name)
except FileNotFoundError:
print("That directory does not exist")
elif inputing.startswith("touch"):
loi = inputing.split()[1:]
name = loi[0]
self.commands["touch"](self, name)
elif inputing.startswith("rm"):
loi = inputing.split()[1:]
name = loi[0]
self.commands["rm"](self, name)
if __name__ == "__main__": # running the shell
shell = Shell()
shell.run()