linux命令执行过程详解

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux命令执行过程详解相关的知识,希望对你有一定的参考价值。

1、执行命令
输入命令后回车
提请shell程序找到键入命令所对应的可执行程序或者代码,并且由其分期后,提交给内核分配资源将其运行起来。
在shell中可执行的命令有俩类:
内部命令:由shell自带的内部集成命令
help 可以查看内部命令列表

[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 也可以查看命令列表

[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

外部命令:在文件系统路径下有对应的可执行的文件
查看路径命令
一、wheris

[root@centos7 ~]# whereis ls     #多了帮助文档,更加详细
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

如果判断命令是内部命令还是外部命令
命令:type

实例:[root@centos7 ~]# type pwd
pwd is a shell builtin            #内部命令
[root@centos7 ~]# type hostname
hostname is /usr/bin/hostname      #外部命令表示为一个文件

也有可能一个命令既是外部命令也是内部命令,可以这样查看

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

那么问题来了,输入命令是执行内部命令,还是外部命令呢?
命令执行是过程是这样的:
先看下是不是内部命令(内部命令执行速度更快),内部命令优先级更高,然后再去检查hash表(记录外部命令的路径),如果找到,找到后再执行。如果找不到这个命令在磁盘下的路径,就会以下面的方式寻找。
外部命令寻找路径的过程(首次执行)

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

在我们再执行一个命令时,当确认不是内部命令后,就会当成外部命令在以上的目录文件中寻找,找到了(找到就停下)就执行,比如这个:

[root@centos7 bin]# type hostname
hostname is /usr/bin/hostname     #在/usr/bin 下面找到

如果找不到就会报错。

[root@centos7 bin]# lsls
bash: lsls: command not found...     #命令输入有误

再找到这个命令后,就会将这个命令的路径记录在hash表中,再次使用时,就可以很快执行,提高命令调用速率。
实例:

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

再次查看命令:

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

清除hash缓存

[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

以上是关于linux命令执行过程详解的主要内容,如果未能解决你的问题,请参考以下文章

linux tftp命令详解

Linux updatedb命令详解

linux top命令详解

linux shell脚本执行命令详解

Linux中top命令输出指标详解

Linux下的crontab定时执行任务命令详解