Detailed description of linux command execution process

Posted by Stu on Sat, 17 Aug 2019 14:10:29 +0200

1. Execution of orders
Enter the command and return
The shell program is asked to find the executable program or code corresponding to the typing command and submit it to the kernel to allocate resources to run it in stages.
There are two types of commands that can be executed in the shell:
Internal commands: Internal integration commands that come with the shell
help can view the list of internal commands

[root@centos7 ~]# help
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
These shell commands are defined internally.  Type `help' to see this list.
Type `help name' to find out more about the function `name'.
Use `info bash' to find out more about the shell in general.
Use `man -k' or `info' to find out more about commands not in this list.

A star (*) next to a name means that the command is disabled.

 job_spec [&]                                                    history [-c] [-d offset] [n] or history -anrw [filename] or >
 (( expression ))                                                if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; >
 . filename [arguments]                                          jobs [-lnprs] [jobspec ...] or jobs -x command [args]
 :                                                               kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... o>
 [ arg... ]                                                      let arg [arg ...]
 [[ expression ]]                                                local [option] name[=value] ...
 alias [-p] [name[=value] ... ]                                  logout [n]
 bg [job_spec ...]                                               mapfile [-n count] [-O origin] [-s count] [-t] [-u fd] [-C c>
 bind [-lpvsPVS] [-m keymap] [-f filename] [-q name] [-u name]>  popd [-n] [+N | -N]
 break [n]                                                       printf [-v var] format [arguments]
 builtin [shell-builtin [arg ...]]                               pushd [-n] [+N | -N | dir]
 caller [expr]                                                   pwd [-LP]
 case WORD in [PATTERN [| PATTERN]...) COMMANDS ;;]... esac      read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N >
 cd [-L|[-P [-e]]] [dir]                                         readarray [-n count] [-O origin] [-s count] [-t] [-u fd] [-C>
 command [-pVv] command [arg ...]                                readonly [-aAf] [name[=value] ...] or readonly -p
 compgen [-abcdefgjksuv] [-o option]  [-A action] [-G globpat]>  return [n]
 complete [-abcdefgjksuv] [-pr] [-DE] [-o option] [-A action] >  select NAME [in WORDS ... ;] do COMMANDS; done
 compopt [-o|+o option] [-DE] [name ...]                         set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]
 continue [n]                                                    shift [n]
 coproc [NAME] command [redirections]                            shopt [-pqsu] [-o] [optname ...]
 declare [-aAfFgilrtux] [-p] [name[=value] ...]                  source filename [arguments]
 dirs [-clpv] [+N] [-N]                                          suspend [-f]
 disown [-h] [-ar] [jobspec ...]                                 test [expr]
 echo [-neE] [arg ...]                                           time [-p] pipeline
 enable [-a] [-dnps] [-f filename] [name ...]                    times
 eval [arg ...]                                                  trap [-lp] [[arg] signal_spec ...]
 exec [-cl] [-a name] [command [arguments ...]] [redirection .>  true
 exit [n]                                                        type [-afptP] name [name ...]
 export [-fn] [name[=value] ...] or export -p                    typeset [-aAfFgilrtux] [-p] name[=value] ...
 false                                                           ulimit [-SHacdefilmnpqrstuvx] [limit]
 fc [-e ename] [-lnr] [first] [last] or fc -s [pat=rep] [comma>  umask [-p] [-S] [mode]
 fg [job_spec]                                                   unalias [-a] name [name ...]
 for NAME [in WORDS ... ] ; do COMMANDS; done                    unset [-f] [-v] [name ...]
 for (( exp1; exp2; exp3 )); do COMMANDS; done                   until COMMANDS; do COMMANDS; done
 function name { COMMANDS ; } or name () { COMMANDS ; }          variables - Names and meanings of some shell variables
 getopts optstring name [arg]                                    wait [id]
 hash [-lr] [-p pathname] [-dt] [name ...]                       while COMMANDS; do COMMANDS; done
 help [-dms] [pattern ...]                                       { COMMANDS ; }

enable can also view a list of commands

[root@centos7 ~]# enable
enable .
enable :
enable [
enable alias
enable bg
enable bind
enable break
enable builtin
enable caller
enable cd
enable command
enable compgen
enable complete
enable compopt
enable continue
enable declare
enable dirs
enable disown
enable echo
enable enable
enable eval
enable exec
enable exit
enable export
enable false
enable fc
enable fg
enable getopts
enable hash
enable help
enable history
enable jobs
enable kill
enable let
enable local
enable logout
enable mapfile
enable popd
enable printf
enable pushd
enable pwd
enable read
enable readarray
enable readonly
enable return
enable set
enable shift

External commands: There are corresponding executable files in the file system path
View Path Command
I. wheris

[root@centos7 ~]# whereis ls     #More Help Documents, More Details
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz

Which-a

[root@centos7 ~]# which -a ls
alias ls='ls --color=auto'
    /usr/bin/ls

If you decide whether the command is internal or external
Command:type

Example:[root@centos7 ~]# type pwd
pwd is a shell builtin            #Internal orders
[root@centos7 ~]# type hostname
hostname is /usr/bin/hostname      #External commands are represented as a file

It is also possible that a command is both an external and an internal command, which can be viewed in this way.

[root@centos7 ~]# type -a pwd
pwd is a shell builtin
pwd is /usr/bin/pwd

So the question arises, is the input command internal or external?
The process of command execution is as follows:
See if internal commands are faster to execute and higher priority to internal commands. Then check the hash table (record the path of external commands). If it is found, it will be executed later. If you can't find the path of this command under disk, you'll find it in the following way.
External command path finding process (first execution)

[root@centos7 bin]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

When we execute another command, when we confirm that it is not an internal command, we will look for it as an external command in the above directory file and execute it when we find it, such as this:

[root@centos7 bin]# type hostname
hostname is /usr/bin/hostname     #Find it under / usr/bin

If you can't find it, you will make an error.

[root@centos7 bin]# lsls
bash: lsls: command not found...     #Error in command input

Once the command is found, the path of the command is recorded in the hash table. When it is used again, it can be executed quickly and the command invocation rate can be increased.
Example:

root@centos7 bin]# hash
hits    command
   2    /usr/bin/whereis
   2    /usr/bin/man
   3    /usr/bin/ls

Look at the command again:

[root@centos7 bin]# type whereis
whereis is hashed (/usr/bin/whereis)
[root@centos7 bin]# type ifconfig
ifconfig is hashed (/usr/sbin/ifconfig)

Clear hash cache

[root@centos7 etc]# hash
hits    command
   6    /usr/sbin/ifconfig
   1    /usr/bin/rm
   1    /usr/bin/cat
   9    /usr/bin/whereis
   3    /usr/bin/cp
   2    /usr/bin/man
   4    /usr/bin/ls
   1    /usr/bin/clear
[root@centos7 etc]# hash -r
[root@centos7 etc]# hash
hash: hash table empty

Topics: Linux shell