linux getdents 例子,Linux内建命令和外部命令(整理)
Linux命令有內(nèi)部命令(內(nèi)建命令)和外部命令之分,內(nèi)部命令和外部命令功能基本相同,但也有些細微差別。
【內(nèi)部命令 vs. 外部命令】
(1)內(nèi)部命令實際上是shell程序的一部分,其中包含的是一些比較簡單的linux系統(tǒng)命令,這些命令由shell程序識別并在shell程序內(nèi)部完成運行,通常在linux系統(tǒng)加載運行時shell就被加載并駐留在系統(tǒng)內(nèi)存中。內(nèi)部命令是寫在bashy源碼里面的,其執(zhí)行速度比外部命令快,因為解析內(nèi)部命令shell不需要創(chuàng)建子進程。比如:exit,history,cd,echo等。常見的內(nèi)部命令詳細見:bash內(nèi)部命令 - koko7958的專欄 - 博客頻道 - CSDN.NET。
(2)外部命令是linux系統(tǒng)中的實用程序部分,因為實用程序的功能通常都比較強大,所以其包含的程序量也會很大,在系統(tǒng)加載時并不隨系統(tǒng)一起被加載到內(nèi)存中,而是在需要時才將其調(diào)用內(nèi)存。通常外部命令的實體并不包含在shell中,但是其命令執(zhí)行過程是由shell程序控制的。shell程序管理外部命令執(zhí)行的路徑查找、加載存放,并控制命令的執(zhí)行。外部命令是在bash之外額外安裝的,通常放在/bin,/usr/bin,/sbin,/usr/sbin......等等。可通過“echo $PATH”命令查看外部命令的存儲路徑,比如:ls、vi等。
(3)type命令可以分辨內(nèi)部命令與外部命令
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.
常用的三個參數(shù):-t對應-type,-a對應-all,-p對應-path,使用:
type [-a |?-t | -p] name 或
type [-all | -type | -path] name。
(1)沒有參數(shù)的狀況下,它會顯示出shell如何解譯name做為命令。
(2)如果有"-type",它將會顯示alias、 keyword、function、builtin或file。
file:表示為外部指令;alias:表示該指令為命令別名所設定的名稱;builtin:表示該指令為 bash 內(nèi)建的指令功能。
(3)如果有"-path"的參數(shù),它將會顯示該命令的完整檔名(外部指令)或顯示為內(nèi)建指令,找不到的話,不顯示任何東西。
(4)如果有"-all"的參數(shù),會將由PATH變量定義的路徑中所有含有name指令的路徑都列出來,即顯示所有可執(zhí)行name的可能路徑。
例如1:
holybin@localhost:~$ type cd
cd is a shell builtin #這里可以看出cd是一個內(nèi)部命令
例如2:
holybin@localhost:~$ type ls
ls is aliased to `ls --color=auto' # 沒有加上任何參數(shù),僅列出 ls 這個指令的最主要使用情況
holybin@localhost:~$ type -t ls
alias # -t 參數(shù)則僅列出 ls 這個指令的最主要使用情況說明
例如3:
holybin@localhost:~$ type -a ls
ls is aliased to `ls --color=auto'
ls is /bin/ls # 列出所有信息:這里可以看出ls是一個外部命令。
【命令與系統(tǒng)調(diào)用】
通常情況下,腳本中的Bash內(nèi)建命令在運行的時候是不會fork出一個子進程的。但是腳本中的外部或者過濾命令通常會fork出一個子進程。 一個內(nèi)建命令通常會與一個系統(tǒng)命令同名,但是Bash在內(nèi)部重新實現(xiàn)了這些命令。比如,Bash的echo命令與/bin/echo就不盡相同,雖然它們的行為在絕大多數(shù)情況下都是一樣的。
例如ls命令,如果是內(nèi)建命令的話應該會引起fork()和exec()這兩個系統(tǒng)調(diào)用,具體的步驟可以參考:ls命令是怎樣實現(xiàn)的,getdents64,linux-2.6.27.5 - 小默 - C++博客,大致的思路就應該是去讀取當前文件夾的目錄項來獲得該目錄下的所有文件名。
其他的命令沒有研究過,望研究過的人提示一下。
【命令與環(huán)境變量】
以pwd命令為例子說明:
(1)pwd命令用于顯示當前工作目錄,也可以使用pwd命令來判斷目錄在文件系統(tǒng)內(nèi)的位置。pwd指令主要跟三個環(huán)境變量:PATH、OLDPWD、PWD有關。
PATH—執(zhí)行文件的路徑。使用"echo $PATH"顯示PATH的內(nèi)容(PATH前面加$表示后面接的是變量),下同;OLDPWD—前一次的工作目錄;PWD—當前的工作目錄。
(2)當我們在shell中輸入“man pwd”時可以看到pwd的幫助文檔。但是輸入“pwd --help”卻是如下錯誤提示:
holybin@localhost:~$ pwd --help
-bash: pwd: --: invalid option
pwd: usage: pwd [-LP]
但是輸入“ /bin/pwd --help”就能正常顯示pwd的幫助文檔:
holybin@localhost:~$ /bin/pwd --help
Usage: /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 exit
NOTE: your shell may have its own version of pwd, which usually supersedes
the version described here. Please refer to your shell's documentation
for details about the options it supports.
Report pwd bugs to bug-coreutils@gnu.org
GNU coreutils home page:
General help using GNU software:
For complete documentation, run: info coreutils 'pwd invocation'
這主要是由于內(nèi)部命令和外部命令的區(qū)別,pwd是內(nèi)部命令,而/bin/pwd就是外部命令。顯示當前目錄時,/bin/pwd能更加準確地顯示當前工作目錄的完整文件路徑,比如在多人共享同一臺Linux機器時,經(jīng)常會發(fā)現(xiàn)當前目錄被別人刪除后,pwd命令仍然會顯示那個目錄,而/bin/pwd則不會。
(3)進一步地,在root權限下將/bin目錄下的ls命令的執(zhí)行文件移動到別目錄中去,例如:mv /bin/ls /root,會發(fā)現(xiàn)無論在哪里都無法順利執(zhí)行l(wèi)s命令。原因在于新的目錄“/root”并不在PATH指定的路徑中,而外部命令必須通過PATH指定的路徑來找到命令對應的執(zhí)行文件。可以使用PATH="$PATH":/root將新的目錄“/root”加入PATH環(huán)境變量。
(4)shell命令解釋器在執(zhí)行命令時,先嘗試按照內(nèi)部命令來執(zhí)行,如果要執(zhí)行的命令不是內(nèi)部命令,則按照外部命令去查找對應的執(zhí)行文件所在的目錄,并執(zhí)行。當要執(zhí)行的命令不是內(nèi)部命令時(例如ls),如果有兩個ls指令分別在不同的目錄中(例如/usr/local/bin/ls和/bin/ls),shell命令解釋器就根據(jù)PATH里面哪個目錄先被查詢到,則那個目錄下的命令就先被執(zhí)行。
參考:區(qū)分Linux內(nèi)建命令和外部命令,linux的內(nèi)建命令和外部命令
總結
以上是生活随笔為你收集整理的linux getdents 例子,Linux内建命令和外部命令(整理)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 颜值以一打十 标致4008 BLACKP
- 下一篇: linux怎么删除exe文件夹,ubun