Linux内建命令和外部命令(整理)

来源:互联网 发布:网络百科全书是什么 编辑:程序博客网 时间:2024/06/05 02:28

Linux命令有内部命令(内建命令)和外部命令之分,内部命令和外部命令功能基本相同,但也有些细微差别。

【内部命令 vs. 外部命令】

(1)内部命令实际上是shell程序的一部分,其中包含的是一些比较简单的linux系统命令,这些命令由shell程序识别并在shell程序内部完成运行,通常在linux系统加载运行时shell就被加载并驻留在系统内存中。内部命令是写在bashy源码里面的,其执行速度比外部命令快,因为解析内部命令shell不需要创建子进程。比如:exit,history,cd,echo等。常见的内部命令详细见:bash内部命令 - koko7958的专栏 - 博客频道 - CSDN.NET。

(2)外部命令是linux系统中的实用程序部分,因为实用程序的功能通常都比较强大,所以其包含的程序量也会很大,在系统加载时并不随系统一起被加载到内存中,而是在需要时才将其调用内存。通常外部命令的实体并不包含在shell中,但是其命令执行过程是由shell程序控制的。shell程序管理外部命令执行的路径查找、加载存放,并控制命令的执行。外部命令是在bash之外额外安装的,通常放在/bin,/usr/bin,/sbin,/usr/sbin......等等。可通过“echo $PATH”命令查看外部命令的存储路径,比如:ls、vi等。

(3)type命令可以分辨内部命令与外部命令

type: type [-afptP] name [name ...]    Display information about command type.        For each NAME, indicate how it would be interpreted if used as a    command name.        Options:      -a        display all locations containing an executable named NAME;        includes aliases, builtins, and functions, if and only if        the `-p' option is not also used      -f        suppress shell function lookup      -P        force a PATH search for each NAME, even if it is an alias,        builtin, or function, and returns the name of the disk file        that would be executed      -p        returns either the name of the disk file that would be executed,        or nothing if `type -t NAME' would not return `file'.      -t        output a single word which is one of `alias', `keyword',        `function', `builtin', `file' or `', if NAME is an alias, shell        reserved word, shell function, shell builtin, disk file, or not        found, respectively        Arguments:      NAME      Command name to be interpreted.        Exit Status:    Returns success if all of the NAMEs are found; fails if any are not found.
常用的三个参数:-t对应-type,-a对应-all,-p对应-path,使用:type [-a | -t | -p] name 或 type [-all | -type | -path] name。

(1)没有参数的状况下,它会显示出shell如何解译name做为命令。

(2)如果有"-type",它将会显示alias、 keyword、function、builtin或file。

         file:表示为外部指令;
         alias:表示该指令为命令别名所设定的名称;
         builtin:表示该指令为 bash 内建的指令功能。

(3)如果有"-path"的参数,它将会显示该命令的完整档名(外部指令)或显示为内建指令,找不到的话,不显示任何东西。

(4)如果有"-all"的参数,会将由PATH变量定义的路径中所有含有name指令的路径都列出来,即显示所有可执行name的可能路径。

例如1:

holybin@localhost:~$ type cdcd is a shell builtin #这里可以看出cd是一个内部命令

例如2:

holybin@localhost:~$ type lsls is aliased to `ls --color=auto' # 没有加上任何参数,仅列出 ls 这个指令的最主要使用情况

holybin@localhost:~$ type -t lsalias # -t 参数则仅列出 ls 这个指令的最主要使用情况说明

例如3:

holybin@localhost:~$ type -a lsls is aliased to `ls --color=auto'ls is /bin/ls # 列出所有信息:这里可以看出ls是一个外部命令。

【命令与系统调用】

通常情况下,脚本中的Bash内建命令在运行的时候是不会fork出一个子进程的。但是脚本中的外部或者过滤命令通常会fork出一个子进程。 一个内建命令通常会与一个系统命令同名,但是Bash在内部重新实现了这些命令。比如,Bash的echo命令与/bin/echo就不尽相同,虽然它们的行为在绝大多数情况下都是一样的。

例如ls命令,如果是内建命令的话应该会引起fork()和exec()这两个系统调用,具体的步骤可以参考:ls命令是怎样实现的,getdents64,linux-2.6.27.5 - 小默 - C++博客,大致的思路就应该是去读取当前文件夹的目录项来获得该目录下的所有文件名。

其他的命令没有研究过,望研究过的人提示一下。

【命令与环境变量】

以pwd命令为例子说明:

(1)pwd命令用于显示当前工作目录,也可以使用pwd命令来判断目录在文件系统内的位置。pwd指令主要跟三个环境变量:PATH、OLDPWD、PWD有关。

PATH—执行文件的路径。使用"echo $PATH"显示PATH的内容(PATH前面加$表示后面接的是变量),下同;OLDPWD—前一次的工作目录;PWD—当前的工作目录。

(2)当我们在shell中输入“man pwd”时可以看到pwd的帮助文档。但是输入“pwd --help”却是如下错误提示:

holybin@localhost:~$ pwd --help-bash: pwd: --: invalid optionpwd: usage: pwd [-LP]

但是输入“ /bin/pwd --help”就能正常显示pwd的帮助文档:

holybin@localhost:~$ /bin/pwd --helpUsage: /bin/pwd [OPTION]...Print the full filename of the current working directory.  -L, --logical   use PWD from environment, even if it contains symlinks  -P, --physical  avoid all symlinks      --help     display this help and exit      --version  output version information and exitNOTE: your shell may have its own version of pwd, which usually supersedesthe version described here.  Please refer to your shell's documentationfor details about the options it supports.Report pwd bugs to bug-coreutils@gnu.orgGNU coreutils home page: <http://www.gnu.org/software/coreutils/>General help using GNU software: <http://www.gnu.org/gethelp/>For complete documentation, run: info coreutils 'pwd invocation'

这主要是由于内部命令和外部命令的区别,pwd是内部命令,而/bin/pwd就是外部命令。显示当前目录时,/bin/pwd能更加准确地显示当前工作目录的完整文件路径,比如在多人共享同一台Linux机器时,经常会发现当前目录被别人删除后,pwd命令仍然会显示那个目录,而/bin/pwd则不会。

(3)进一步地,在root权限下将/bin目录下的ls命令的执行文件移动到别目录中去,例如:mv /bin/ls /root,会发现无论在哪里都无法顺利执行ls命令。原因在于新的目录“/root”并不在PATH指定的路径中,而外部命令必须通过PATH指定的路径来找到命令对应的执行文件。可以使用PATH="$PATH":/root将新的目录“/root”加入PATH环境变量。

(4)shell命令解释器在执行命令时,先尝试按照内部命令来执行,如果要执行的命令不是内部命令,则按照外部命令去查找对应的执行文件所在的目录,并执行。当要执行的命令不是内部命令时(例如ls),如果有两个ls指令分别在不同的目录中(例如/usr/local/bin/ls和/bin/ls),shell命令解释器就根据PATH里面哪个目录先被查询到,则那个目录下的命令就先被执行。


参考:区分Linux内建命令和外部命令,linux的内建命令和外部命令


0 0
原创粉丝点击