Linux is执行程序命令,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
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的Linux is执行程序命令,linux命令执行过程详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 二抵是什么意思
- 下一篇: linux awk命令用途,Linux: