输入命令后回车
提请shell程序找到键入命令所对应的可执行程序或者代码,并且由其分期后,提交给内核分配资源将其运行起来。
在shell中可执行的命令有俩类:
内部命令:由shell自带的内部集成命令
help 可以查看内部命令列表
[[email protected] ~]# helpGNU 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 也可以查看命令列表
[[email protected] ~]# enableenable .enable :enable [enable aliasenable bgenable bindenable breakenable builtinenable callerenable cdenable commandenable compgenenable completeenable compoptenable continueenable declareenable dirsenable disownenable echoenable enableenable evalenable execenable exitenable exportenable falseenable fcenable fgenable getoptsenable hashenable helpenable historyenable jobsenable killenable letenable localenable logoutenable mapfileenable popdenable printfenable pushdenable pwdenable readenable readarrayenable Readonlyenable returnenable setenable shift
外部命令:在文件系统路径下有对应的可执行的文件
查看路径命令
一、wheris
[[email protected] ~]# whereis ls #多了帮助文档,更加详细ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
二、which -a
[[email protected] ~]# which -a lsalias ls=‘ls --color=auto‘ /usr/bin/ls
如果判断命令是内部命令还是外部命令
命令:type
实例:[[email protected] ~]# type pwdpwd is a shell builtin #内部命令[[email protected] ~]# type hostnamehostname is /usr/bin/hostname #外部命令表示为一个文件
也有可能一个命令既是外部命令也是内部命令,可以这样查看
[[email protected] ~]# type -a pwdpwd is a shell builtinpwd is /usr/bin/pwd
那么问题来了,输入命令是执行内部命令,还是外部命令呢?
命令执行是过程是这样的:
先看下是不是内部命令(内部命令执行速度更快),内部命令优先级更高,然后再去检查hash表(记录外部命令的路径),如果找到,找到后再执行。如果找不到这个命令在磁盘下的路径,就会以下面的方式寻找。
外部命令寻找路径的过程(首次执行)
[[email protected] bin]# echo $PATH/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
在我们再执行一个命令时,当确认不是内部命令后,就会当成外部命令在以上的目录文件中寻找,找到了(找到就停下)就执行,比如这个:
[[email protected] bin]# type hostnamehostname is /usr/bin/hostname #在/usr/bin 下面找到
如果找不到就会报错。
[[email protected] bin]# lslsbash: lsls: command not found... #命令输入有误
再找到这个命令后,就会将这个命令的路径记录在hash表中,再次使用时,就可以很快执行,提高命令调用速率。
实例:
[email protected] bin]# hashhits command 2 /usr/bin/whereis 2 /usr/bin/man 3 /usr/bin/ls
再次查看命令:
[[email protected] bin]# type whereiswhereis is hashed (/usr/bin/whereis)[[email protected] bin]# type ifconfigifconfig is hashed (/usr/sbin/ifconfig)
清除hash缓存
[[email protected] etc]# hashhits 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[[email protected] etc]# hash -r[[email protected] etc]# hashhash: hash table empty总结
以上是内存溢出为你收集整理的linux命令执行过程详解全部内容,希望文章能够帮你解决linux命令执行过程详解所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)