4
4
import re
5
5
import shlex
6
6
7
- COMMANDS = ['exit' , 'echo' , 'type' , 'pwd' , 'cd' , 'cat' , 'exe' ]
7
+ COMMANDS = {
8
+ 'exit' : 'exit_command' ,
9
+ 'echo' : 'echo_command' ,
10
+ 'type' : 'type_command' ,
11
+ 'pwd' : 'pwd_command' ,
12
+ 'cd' : 'cd_command' ,
13
+ 'cat' : 'cat_command' ,
14
+ 'exe' : 'exe_command'
15
+ }
8
16
9
17
path_variable = os .environ .get ("PATH" )
10
18
path_list = path_variable .split (':' )
11
19
12
- def cat (splitted ):
20
+ def cat_command (splitted , command ):
13
21
"""
14
22
This function handles the 'cat' command. It attempts to read files from
15
23
the specified directories and prints their contents if they exist.
16
24
"""
25
+ splitted = shlex .split (command [4 :])
17
26
result = []
18
27
for path in splitted :
19
28
directory , target_file = os .path .split (path )
@@ -44,11 +53,12 @@ def cat(splitted):
44
53
else :
45
54
print ("No files were found." )
46
55
47
- def exe ( splitted ):
56
+ def exe_command ( splitted_command , command ):
48
57
"""
49
58
This function handles the 'exe' command. It attempts to open and read the
50
59
contents of the specified file.
51
60
"""
61
+ splitted = splitted_command [- 1 ]
52
62
try :
53
63
with open (splitted , 'r' ) as file :
54
64
contents = file .read ()
@@ -58,18 +68,19 @@ def exe(splitted):
58
68
except Exception as e :
59
69
print (f"An error occurred: { e } " )
60
70
61
- def pwd ( ):
71
+ def pwd_command ( splitted_command , command ):
62
72
"""
63
73
This function handles the 'pwd' command. It prints the current working directory.
64
74
"""
65
75
current_path = os .getcwd ()
66
76
print (current_path )
67
77
68
- def cd ( new_path ):
78
+ def cd_command ( splitted_command , command ):
69
79
"""
70
80
This function handles the 'cd' command. It attempts to change the current
71
81
directory to the specified path.
72
82
"""
83
+ new_path = splitted_command [1 ]
73
84
if new_path == '~' :
74
85
home_directory = os .environ .get ('HOME' )
75
86
os .chdir (home_directory )
@@ -78,15 +89,15 @@ def cd(new_path):
78
89
else :
79
90
print (f"cd: { new_path } : No such file or directory" )
80
91
81
- def echo ( command ):
92
+ def echo_command ( splitted_command , command ):
82
93
"""
83
94
This function handles the 'echo' command. It splits the command and prints
84
95
the arguments passed to echo.
85
96
"""
86
97
x = shlex .split (command , posix = True )
87
98
print (" " .join (x [1 :]))
88
99
89
- def type_command (splitted_command ):
100
+ def type_command (splitted_command , command ):
90
101
"""
91
102
This function handles the 'type' command. It checks whether the specified
92
103
command is a shell builtin or a file and prints the corresponding message.
@@ -104,8 +115,15 @@ def type_command(splitted_command):
104
115
main ()
105
116
print (f"{ splitted_command [1 ]} : not found" )
106
117
118
+ def exit_command (splitted_command , command ):
119
+ """
120
+ This function handles the 'exit' command and exits the program if the argument is '0'.
121
+ """
122
+ if len (splitted_command ) == 2 and splitted_command [1 ] == "0" :
123
+ exit (0 )
124
+
107
125
def main ():
108
-
126
+
109
127
sys .stdout .write ("$ " )
110
128
111
129
# Wait for user input
@@ -117,25 +135,10 @@ def main():
117
135
if "_" in splitted_command [0 ] and len (splitted_command ) == 2 :
118
136
# Execute a subprocess if the command is a valid executable
119
137
subprocess .run ([splitted_command [0 ], splitted_command [1 ]])
120
- elif splitted_command [0 ] not in COMMANDS :
138
+ elif splitted_command [0 ] in COMMANDS :
139
+ globals ()[COMMANDS [splitted_command [0 ]]](splitted_command , command )
140
+ else :
121
141
print (f"{ splitted_command [0 ]} : command not found" )
122
- elif splitted_command [0 ] == "exe" :
123
- splitted = splitted_command [- 1 ]
124
- exe (splitted )
125
- elif splitted_command [0 ] == "cat" :
126
- splitted = shlex .split (command [4 :])
127
- cat (splitted )
128
- elif splitted_command [0 ] == "pwd" :
129
- pwd ()
130
- elif splitted_command [0 ] == "cd" :
131
- new_path = splitted_command [1 ]
132
- cd (new_path )
133
- elif splitted_command [0 ] == "exit" and len (splitted_command ) == 2 and splitted_command [1 ] == "0" :
134
- exit (0 )
135
- elif splitted_command [0 ] == "echo" :
136
- echo (command )
137
- elif splitted_command [0 ] == "type" :
138
- type_command (splitted_command )
139
142
140
143
main ()
141
144
0 commit comments