Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Linux command execution process

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

1. View the default shell type used by the CentOS7 system [root@CentOS7 ~] # echo $SHELL/bin/bash

The default is bash, which is loaded into memory when the system starts. Typically, we refer to commands integrated in shell as internal commands, which can be run directly.

Command types in 2.shell

Internal commands: integrated in shell, can be run directly in memory.

External command: appears as a disk file that displays the full path. The external command is not loaded into memory at first, but when the user executes the command, the system temporarily uses the PATH variable to find the path of the external command from the disk and load it into memory to run. 3. How to determine the type of a command [root @ CentOS7 /] # help typetype: type [- afptP] name [name.] Display information about command type.

For example:

[root@CentOS7 ~] # type moremore is / usr/bin/more [root@CentOS7 ~] # type pwdpwd is a shell builtin

From the type divided above, you can see that more is an external command and pwd is an internal command.

Ps: internal commands are integrated in shell, and the system gives priority to internal commands.

4. Internal command management tool enable [root@CentOS7 /] # help enableenable: enable [- a] [- dnps] [- f filename] [name...] Enable and disable shell builtins.

a. View internal commands

[root@CentOS7 ~] # enableenable .enable: enable [enable aliasenable bgenable bindenable breakenable builtinenable callerenable cd...

b. Enable internal commands

Enable cmd

c. Disable internal commands or display a list of disabled internal commands

Forbidden

Enable-n cmd [root@CentOS7 ~] # enable-n cd [root@CentOS7 ~] # enable-n echo

Display

[root@CentOS7] # enable-nenable-n cdenable-n echo5. What happens after the external command is executed?

Let's first take a look at the result of judging the occurrence of two external commands with the type command:

[root@CentOS7 ~] # type unameuname is hashed (/ usr/bin/uname) [root@CentOS7 ~] # type ifconfigifconfig is / usr/sbin/ifconfig found that the word hashed was added to the judgment result of the uname command, and why?

Because when executing a command, the system will first determine the type of command to be executed. If it is an internal command, it will run it directly. If it is an external command, it will find the path where the command is located according to the system PATH variable path and then run it, and the command path will be cached in memory after running. When running next time, it will no longer have to search one by one from the PATH path, but will directly find it in memory. This can not only reduce the disk pressure, but also improve the efficiency of command execution. The word hashed does not appear in the judgment of the ifconfig command because the command has not yet been executed.

6. What are the "advantages" of caching commands in memory besides improving execution efficiency? [root@CentOS7 ~] # tty/dev/pts/0 [root@CentOS7 ~] # uname-r3.10.0-957.el7.x86_64 [root@CentOS7 ~] # which uname/usr/bin/uname [root@CentOS7 ~] # mv `!!` / usr/sbin/mv `which uname` / usr/sbin/ [root@CentOS7 ~] # uname-rbash: / usr/bin/uname: No such file or directory [root@CentOS7 ~] # hashhits command 1 / usr/bin/tty 1 / usr/bin/which 1 / usr/bin/mv 2 / usr/bin/uname

Sometimes the path of the command is moved because of some need or misoperation, and the error of "- bash: xxx: No such file or directory" is reported when it is executed again. Why? Analyze it based on the example above:

a. When an external command is executed for the first time, it is cached in memory, and the next time the command is executed, bash will find it directly from memory.

b. However, because the command path is moved and the original path is cached in memory, an error will be reported. Through the above hash command, you can find that the current path of the command is inconsistent with the path cached in memory.

This problem can be resolved in the following ways:

Execute hash-d cmd to delete the location where the command is cached in memory, and execute it again to use the moved path

Use a new virtual terminal or pseudo terminal that has not executed the command, but use the moved path, as shown below:

Ps:hits indicates the number of times the command path has been hit by the cache. 7. Summary

Comparing the difference between internal commands and external commands, it is found that internal commands are more efficient than external commands, because internal commands are directly built into shell, while external commands are stored on disk. But in fact, there is another command that takes precedence over internal commands.

Alias [root@CentOS7 ~] # alias history='uname-r'[root@CentOS7 ~] # history3.10.0-957.el7.x86_64 [root@CentOS7 ~] # hashhits command 1 / usr/bin/uname

You can see that the displayed result is the alias execution, when the alias has the same name as the original command, but when you want to use the original command, you can execute it in the following ways:

'cmd''\ cmd''/usr/bin/cmd' (external command) [root@CentOS7 ~] #\ history 1179\ history 1 [root@CentOS7 ~] #' history' 1180 'history' 1

From the above tests, we can see that the order in which Linux commands are executed is as follows:

Alias > Internal Command > hash Cache external Command > PATH variable path > command not found

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report