"Cd test_dir2 cannot modify our current directory" is correct, but it is also wrong in a sense. After executing this command, we are still in the same directory. In this sense, it is correct. However, the directory has actually been modified, but it is modified in the child process.
We still remember that we fork a sub-process and then execute the command. The process of executing the Command did not happen to the parent process. The result is that we only changed the current directory of the child process, not the directory of the parent process.
Then the child process exits, and the parent process continues to run in the intact directory.
Therefore, such shell-related commands must be built-in commands. It must be executed in the shell process rather than in the forking process ).
Cd
Let's start with the cd command.
First, create a builtins directory. Every built-in command is put into this directory.
yosh_project|-- yosh |-- builtins | |-- __init__.py | |-- cd.py |-- __init__.py |-- shell.py
In cd. py, we use the system to call OS. chdir to implement our cd command.
import osfrom yosh.constants import *def cd(args): os.chdir(args[0]) return SHELL_STATUS_RUN
Note: We will return the shell running status from the built-in function. So, to continue using constants in the project, we move them to yosh/constants. py.
yosh_project|-- yosh |-- builtins | |-- __init__.py | |-- cd.py |-- __init__.py |-- constants.py |-- shell.py
In constants. py, we put all the State constants here.
SHELL_STATUS_STOP = 0SHELL_STATUS_RUN = 1
Now, our built-in cd is ready. Let's modify shell. py to process these built-in functions.
... ### Import constant from yosh. constants import * ### use Hash ing to store the built-in function name and Its Reference built_in_cmds ={} def tokenize (string): return shlex. split (string) def execute (pai_tokens): ### split the command name from the tuples and the parameter pai_name = pai_tokens [0] pai_args = pai_tokens [1:] ### if this command is a built-in command, use the parameter to call this function if using _name in built_in_cmds: return built_in_cmds [using _name] (using _args )...
We use a python dictionary variable built_in_cmdsHash ingTo store our built-in functions. We extract the command name and parameters from the execute function. If this command is in our hash ing, the corresponding built-in function is called.
(Note: built_in_cmds [using _name] returns a function reference that can be called directly using parameters .)
We are almost ready to use the built-in cd function. The last step is to add the cd function to built_in_cmds ing.
We have defined the register_command function to add a built-in function to our built-in command hash ing. Next, we will define the init function and register the built-in cd function here.
Pay attention to this line of register_command ("cd", cd ). The first parameter is the name of the command. The second parameter is a function reference. To make the second parameter cd reference yosh/builtins/cd. cd function reference in py. We must put the following line of code in yosh/builtins/_ init __. in The py file.
from yosh.builtins.cd import *
Therefore, in yosh/shell. py, when we import * From yosh. builtins, we can get the reference of the cd function that has been imported through yosh. builtins.
We have prepared the code. Let's try to run our shell, python-m yosh. shell in the form of modules in the yosh directory at the same level.
Now, the cd command can correctly modify our shell directory, while non-built-in commands can still work. Very good!
Exit
The last piece finally came: Exit elegantly.
We need a function that can modify the shell state to SHELL_STATUS_STOP. In this way, the shell loop can end naturally, and the shell will exit when it reaches the end.
Like cd, If we fork and execute the exit function in the child process, it does not work for the parent process. Therefore, the exit function must be a shell built-in function.
Let's start from this: Create a new file named exit. py in the builtins directory.
yosh_project|-- yosh |-- builtins | |-- __init__.py | |-- cd.py | |-- exit.py |-- __init__.py |-- constants.py |-- shell.py
Exit. py defines an exit function, which returns only one State that can exit the main loop.
from yosh.constants import *def exit(args): return SHELL_STATUS_STOP
Then, import the exit function reference in the yosh/builtins/_ init _. py file.
from yosh.builtins.cd import *from yosh.builtins.exit import *
Finally, register the exit command in the init () function of shell. py.
... #### Register all built-in commands here def init (): register_command ("cd", cd) register_command ("exit", exit )...
So far!
Run python-m yosh. shell. Now you can enter exit to exit the program elegantly.
Last thought
I hope you can enjoy creating yosh (YourOwnShell. However, my yosh version is still in its early stages. I have not dealt with some extreme situations that will cause shell crash. There are also many built-in commands that I have not covered. To improve performance, some non-built-in commands can also be implemented as built-in commands (to avoid the creation time of new processes ). At the same time, a large number of features have not been implemented (please refer to the public features and different features ).
Now it's time to create your own Shell.
Happy Coding!
From: https://linux.cn: 443/article-7625-1.html
Address: http://www.linuxprobe.com/python-shell-create.html