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
How do I read the source code of shell commands?
提问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 coreutils
package.
所有这些基本命令都是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 ls
command:
说实话,在这里你可以找到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
回答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 sudo
assuming your downloading to a location you own.
您应该能够在 ubuntu 上使用这样的命令来收集包的源代码,您可以省略sudo
假设您下载到您拥有的位置。
回答by Drew Noakes
Direct links to source for some popular programs in coreutils
:
一些流行程序的源代码的直接链接coreutils
:
cat
(767 lines)chmod
(570 lines)cp
(2912 lines)cut
(831 lines)date
(570 lines)df
(1718 lines)du
(1112 lines)echo
(272 lines)head
(1070 lines)hostname
(116 lines)kill
(312 lines)ln
(651 lines)ls
(4954 lines)md5sum
(878 lines)mkdir
(306 lines)mv
(512 lines)nice
(220 lines)pwd
(394 lines)rm
(356 lines)rmdir
(252 lines)shred
(1325 lines)tail
(2301 lines)tee
(220 lines)touch
(437 lines)wc
(801 lines)whoami
(91 lines)
回答by hurufu
Actually more sane sources are provided by http://suckless.orglook at their sbase
repository:
实际上,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.c
has 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, ls
source code has now 5309 lines
自 2012 年以来情况发生了变化,ls
源代码现在有 5309 行