Linux 如何阅读shell命令的源代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11528267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 13:49:59  来源:igfitidea点击:

How do I read the source code of shell commands?

linuxcommandgnu-coreutils

提问by Luigi Tiburzi

I would like to read the actual source code which the linux commands are written with. I've gained some experience using them and now I think it's time to interact with my machine at a deeper level.

我想阅读编写 linux 命令的实际源代码。我已经获得了一些使用它们的经验,现在我认为是时候在更深层次上与我的机器进行交互了。

I've found some commands here http://directory.fsf.org/wiki/GNU. Unfortunately I wasn't able to find basic commands such as 'ls' which seems to me easy enough to begin.

我在这里找到了一些命令http://directory.fsf.org/wiki/GNU。不幸的是,我无法找到诸如“ls”之类的基本命令,这在我看来很容易上手。

How exactly do I read the source code of the simple shell commands like 'ls'? I'm running on Ubuntu 12.04

我究竟如何阅读像“ls”这样的简单 shell 命令的源代码?我在 Ubuntu 12.04 上运行

采纳答案by Zagorax

All these basic commands are part of the coreutilspackage.

所有这些基本命令都是coreutils包的一部分。

You can find all information you need here:

您可以在此处找到所需的所有信息:

http://www.gnu.org/software/coreutils/

http://www.gnu.org/software/coreutils/

If you want to download the latest source, you should use git:

如果你想下载最新的源代码,你应该使用 git:

git clone git://git.sv.gnu.org/coreutils

git clone git://git.sv.gnu.org/coreutils

To install git on your Ubuntu machine, you should use apt-get (git is not included in the standard Ubuntu installation):

要在您的 Ubuntu 机器上安装 git,您应该使用 apt-get(标准 Ubuntu 安装中不包含 git):

sudo apt-get install git

sudo apt-get install git

Truth to be told, here you can find specific source for the lscommand:

说实话,在这里你可以找到ls命令的具体来源:

http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/ls.c

http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/ls.c

Only 4984 code lines for a command 'easy enough' as ls... are you still interested in reading it?? Good luck! :D

一个“足够简单”的命令只有 4984 行代码,因为ls……你还有兴趣阅读它吗??祝你好运!:D

回答by Denys Séguret

lsis part of coreutils. You can get it with git :

ls是 coreutils 的一部分。你可以用 git 得到它:

git clone git://git.sv.gnu.org/coreutils

You'll find coreutils listed with other packages (scroll to bottom) on this page.

您会在此页面上找到与其他软件包一起列出的 coreutils(滚动到底部)。

回答by Aaron Peter Samuel

    cd ~ && apt-get source coreutils && ls -d coreutils*     

You should be able to use a command like this on ubuntu to gather the source for a package, you can omit sudoassuming your downloading to a location you own.

您应该能够在 ubuntu 上使用这样的命令来收集包的源代码,您可以省略sudo假设您下载到您拥有的位置。

回答by Drew Noakes

Direct links to source for some popular programs in coreutils:

一些流行程序的源代码的直接链接coreutils

Full list here.

完整列表在这里。

回答by hurufu

Actually more sane sources are provided by http://suckless.orglook at their sbaserepository:

实际上,httpsbase: //suckless.org 提供了更多合理的资源,查看他们的存储库:

git clone git://git.suckless.org/sbase

git clone git://git.suckless.org/sbase

They are clearer, smarter, simpler and suckless, eg ls.chas just 369 LOC

它们更清晰、更智能、更简单、更简单,例如ls.c只有 369 个 LOC

After that it will be easier to understand more complicated GNU code.

之后,更容易理解更复杂的 GNU 代码。

回答by slashmais

CoreUtils referred to in other posts does NOT show the real implementation of most of the functionality which I think you seek. In most cases it provides front-ends for the actual functions that retrieve the data, which can be found here:

其他帖子中提到的 CoreUtils 并未显示我认为您寻求的大多数功能的真实实现。在大多数情况下,它为检索数据的实际功能提供前端,可在此处找到:

It is build upon Gnulibwith the actual source code in the lib-subdirectory

它建立在Gnulib 之上,在lib-subdirectory 中有实际的源代码

回答by RMPR

You can have it on github using the command

你可以使用命令在github上拥有它

git clone https://github.com/coreutils/coreutils.git

You can find all the source codes in the src folder.

您可以在 src 文件夹中找到所有源代码。

You need to have gitinstalled.

你需要安装git

Things have changed since 2012, lssource code has now 5309 lines

自 2012 年以来情况发生了变化,ls源代码现在有 5309 行