當前位置: 首頁>>技術教程>>正文


‘cd’ 命令在哪個包中?

,

問題描述

我想查看命令 cd 背後的源代碼,但 cd 似乎沒有自己的包,那麽它在哪個包中?

最佳方法

找出使用 type 命令的命令的類型。

$ type cd
cd is a shell builtin
$ type ls
ls is aliased to `ls --color=auto'
$ type cat
cat is /bin/cat

你可以看到,cd 是一個內置的 shell。

這意味著,它是默認情況下 Bash 的 shell 的一部分。這當然也是它所包含的軟件包。

對於不是 shell 內置程序而是可執行文件的已安裝命令,使用 dpkg -S 查找包:

$ dpkg -S $(which cat)
coreutils: /bin/cat

要獲得有關內置命令的幫助,請使用 help 命令(也是內置命令):

$ help cd
cd: cd [-L|[-P [-e]] [-@]] [dir]
    Change the shell working directory.

[... output shortened ...]

次佳方法

cd 必然是殼 內置。如果 shell 產生一個改變工作目錄然後退出的子進程,則父進程(shell 本身)不會受到影響。

至於 cd 的源代碼,隻需調用 chdir(2) 即可,改變進程的工作目錄。請參閱 opengroup.org 上的 chdir,其中指出:

The chdir() function shall cause the directory named by the pathname pointed to by the path argument to become the current working directory; that is, the starting point for path searches for pathnames not beginning with ‘/’.

第三種方法

這是一個 man page for cd 但它不是官方的,因為 cd 是“Shell 內置命令”的一部分。就像其他一些命令一樣……

alias, bg, bind, break, builtin, command, compgen, complete, 
continue, declare, dirs, disown, echo, enable, eval, exec, exit, 
export, fc, fg, getopts, hash, help, history, jobs, kill, let, local, 
logout, popd, printf, pushd, pwd, read, readonly, return, set, shift, 
shopt, source, suspend, test, times, trap, type, typeset, ulimit,
umask, unalias, unset, wait 

請參閱 bash 的手冊頁。從鏈接 cd 狀態:

cd [-L|-P] [dir]

Change the current directory to dir. The variable HOME is the default dir. The variable CDPATH defines the search path for the directory containing dir. Alternative directory names in CDPATH are separated by a colon (:). A null directory name in CDPATH is the same as the current directory, i.e., ”.”. If dir begins with a slash (/), then CDPATH is not used. The -P option says to use the physical directory structure instead of following symbolic links (see also the -P option to the set builtin command); the -L option forces symbolic links to be followed. An argument of – is equivalent to $OLDPWD. If a non-empty directory name from CDPATH is used, or if – is the first argument, and the directory change is successful, the absolute pathname of the new working directory is written to the standard output. The return value is true if the directory was successfully changed; false otherwise.

Which package is the command ‘cd’ in?

那將是 bash

第四種方法

cd 的實際源代碼是為

  • Bashbuiltins/cd.def 文件末尾

  • TCSHsh.dir.c 中作為函數 dochngd

  • PDKSH(在某些 BSD 中發現的公共域 Korn shell)位於文件頂部的 c_ksh.c

  • src/cmd/ksh93/bltins/cd_pwd.c 中的 Korn 93(原始 Korn shell,舊版本)

sys-call chdir

  • Linux kernelfs/open.c

  • FreeBSD 是這個項目開頭的鏈接指向的地方

  • 其他 BSD-type 操作係統和 OpenSolaris 的工作方式非常相似

可能需要注意的是 sys-calls 仍然不是鏈的末端,它會在單個文件係統級別關閉。

第五種方法

是的,正如每個人所寫的,cd 內置於您使用的任何 shell 中,因為進程不能影響其父進程的工作目錄。 cd 有一些花裏胡哨的東西(我的意思是 pushd 和朋友,cd 沒有爭論,等等),但如果你不厭其煩地挖掘源代碼,你會不知所措:主要是 cd 調用chdir(2) 係統調用。

沒什麽可看的,因為與 HOME 之類的環境變量不同,”working directory” 是一個係統原語:它是每個進程都具有的屬性之一,例如(真實的)用戶 ID 或調度優先級。它是打開具有相對路徑的文件的起點。

參考資料

本文由Ubuntu問答整理, 博文地址: https://ubuntuqa.com/zh-tw/article/11912.html,未經允許,請勿轉載。