Linux命令执行过程
1.查看CentOS7系统默认使用的shell类型[[email?protected] ~]#echo $SHELL /bin/bash 默认是bash,在系统启动时就已经加载到内存中。通常,我们把集成在shell中的命令就称为内部命令,可直接运行。
type[[email?protected] /]#help type type: type [-afptP] name [name ...] Display information about command type. 例如: [[email?protected] ~]#type more more is /usr/bin/more [[email?protected] ~]#type pwd pwd is a shell builtin 根据上面所划分的类型可以看出more是一个外部命令,pwd是一个内部命令。 ps:内部命令集成在shell中,系统优先使用内部命令。
enable[[email?protected] /]#help enable enable: enable [-a] [-dnps] [-f filename] [name ...] Enable and disable shell builtins. a.查看内部命令 [[email?protected] ~]#enable enable . enable : enable [ enable alias enable bg enable bind enable break enable builtin enable caller enable cd ... b.启用内部命令 enable cmd c.禁用内部命令或显示已禁用的内部命令列表 禁用 enable -n cmd [[email?protected] ~]#enable -n cd [[email?protected] ~]#enable -n echo 显示 [[email?protected] ~]#enable -n enable -n cd enable -n echo
先来看下用type命令判断两个外部命令发生的结果: [[email?protected] ~]#type uname uname is hashed (/usr/bin/uname) [[email?protected] ~]#type ifconfig ifconfig is /usr/sbin/ifconfig 是否发现uname命令的判断结果中多了个hashed字样,这是为啥?因为在执行命令的时候,系统会先判断执行的命令是啥类型,如果是内部命令则直接运行,如果是外部命令就会根据系统PATH变量路径来查找该命令所在的路径然后运行,并且运行后会把该命令路径缓存在内存中,当下次运行时就不用再从PATH路径中挨个搜索了,而是直接在内存中找,这样不仅能减少磁盘压力,而且还能提高命令的执行效率。ifconfig命令的判断结果中之所以没出现hashed字样,是因为这个命令还没有被执行过。
[[email?protected] ~]#tty /dev/pts/0 [[email?protected] ~]#uname -r 3.10.0-957.el7.x86_64 [[email?protected] ~]#which uname /usr/bin/uname [[email?protected] ~]#mv `!!` /usr/sbin/ mv `which uname` /usr/sbin/ [[email?protected] ~]#uname -r bash: /usr/bin/uname: No such file or directory [[email?protected] ~]#hash hits command 1 /usr/bin/tty 1 /usr/bin/which 1 /usr/bin/mv 2 /usr/bin/uname 有时候会因为一些需要或者误操作移动了命令的路径,再次执行的时候就会报“-bash: xxx: No such file or directory”这种错误,这是为啥呢?根据上面的示例来分析一下: a.当外部命令第一次执行后就会被缓存到内存中,当下次再执行这条命令的时候bash就会直接从内存中去找; b.但是,由于命令路径被移动了,而内存中缓存的却是原来的路径,故会报错,通过上面的hash命令来可以发现命令的当前路径和缓存在内存中的路径是不一致的。 可通过以下方法来解决该问题:
ps:hits表示该命令路径被缓存命中的次数。
前面比较了内部命令和外部命令的区别,发现内部命令的执行效率要比外部命令的高,因为内部命令直接内置在shell中,而外部命令是存在磁盘中的。但事实上还有一种命令优先级比内部命令还要高。 别名[[email?protected] ~]#alias history=‘uname -r‘ [[email?protected] ~]#history 3.10.0-957.el7.x86_64 [[email?protected] ~]#hash hits command 1 /usr/bin/uname 可以看出显示的结果是别名执行得到的,当别名和原始命令同名,但是要使用原始命令的时候可以通过以下方式执行:
别名>内部命令>hash缓存外部命令>PATH变量路径>command not found(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |