
我試圖了解該ls
命令是如何運作的,並且我假設有一個 shell 腳本定義了ls
檔案系統中的某個位置。這樣對嗎?如果有的話,我可以在哪裡找到它?
答案1
ls
使用opendir()
和readdir()
逐步瀏覽目錄中的所有文件。如果它需要有關其中之一的更多信息,它會調用stat()
.當然,請閱讀原始程式碼,但一個非常方便的快捷方式是:
# strace ls
有幾個評論的關鍵部分是:
取得目錄條目
open(".", O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY|O_CLOEXEC) = 3
fcntl64(3, F_GETFD) = 0x1 (flags FD_CLOEXEC)
getdents64(3, /* 53 entries */, 32768) = 1744
getdents64(3, /* 0 entries */, 32768) = 0
close(3) = 0
驗證 stdout 是字元設備
fstat64(1, {st_mode=S_IFCHR|0600, st_rdev=makedev(136, 0), ...}) = 0
將標準輸入映射到記憶體中。 (不知道為什麼,看源碼)
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1,
0) = 0xb73ff000
將目錄條目寫入 stdout 和總結
write(1, "bin Desktop Documents Downloa"..., 91bin Desktop
Documents Download Music Pictures Public public_html Templates
Videos
) = 91
close(1) = 0
munmap(0xb73ff000, 4096) = 0
close(2) = 0
exit_group(0) = ?
答案2
ls
不是 shell 腳本,如果你發出file
指令,你會知道它是一個 ELF 64 位元 LSB 執行檔:
$ type -a ls
ls is aliased to `ls --color=auto'
ls is /usr/bin/ls #<---- now we know the file path of `ls`
ls is /bin/ls
$
$ file /usr/bin/ls
/usr/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=ddf8cdb3f1fd2e8263637b7c8ccea84fbf41ee3c, stripped
$
網路上可以找到原始碼這裡。
或者,如果您的發行版是基於 RPM 的 Linux 發行版 + dnf
,那麼您可以:
$ rpm -qf /usr/bin/ls
coreutils-8.22-22.fc21.x86_64 #so now we know the package name is coreutils
$ sudo dnf whatprovides /usr/bin/ls #alternative way
Using metadata from Mon May 16 02:39:55 2016 (1 day, 23:03:50 hours old)
coreutils-8.22-22.fc21.x86_64 : A set of basic GNU tools commonly used in shell scripts
Repo : @System
coreutils-8.22-19.fc21.x86_64 : A set of basic GNU tools commonly used in shell scripts
Repo : fedora
coreutils-8.22-22.fc21.x86_64 : A set of basic GNU tools commonly used in shell scripts
Repo : updates
$
$ mkdir coreutils #optional
$ cd coreutils #optional
$ sudo dnf download --source coreutils
...
$ rpm2cpio coreutils-8.22-22.fc21.src.rpm |cpio -idmv
...
$ sudo rm coreutils-8.22-22.fc21.src.rpm #optional
$ unp coreutils-8.22.tar.xz
...
$ rm coreutils-8.22.tar.xz #optional
$ cd coreutils-8.22/
$ find . -iname 'ls*'
./lib/lseek.c
./lib/lstat.c
./src/ls.c #<---- now we know ls.c is here
./src/ls-vdir.c
./src/ls.h
./src/ls-ls.c
./src/ls-dir.c
./man/ls.x
./tests/ls
./tests/misc/ls-misc.pl
./tests/misc/ls-time.sh
./m4/ls-mntd-fs.m4
./m4/lstat.m4
./m4/lseek.m4
$ vi ./src/ls.c
筆記:
coreutils-8.22-22.fc21.src.rpm 是我的,您的包編號可能有所不同。
有些指令如
type -a history
return“history is a shellbuiltin”,你應該查看目前的shell原始碼,即rpm -qf `readlink -f /proc/$$/exe`
(Detect current shell by command is棘手的比你想像的要多,這個技巧在 shell 中不起作用fish
)在 csh/tcsh shell 中,您應該使用,
where history
因為沒有這樣的type
指令。可以找到更多詳細信息這裡。您可能還有興趣嘗試通配符,例如
repoquery --resolve --archlist=src '*compress*'
包含未安裝的軟體包(如果查詢命令如“*uncompress*”,請小心,在這種情況下,如果第一次嘗試“*uncompress*”,您需要刪除前綴「un」以縮小範圍失敗的)。上面的輸出repoquery
需要刪除中間0:並可選擇後綴.rpm取得可用於搜尋的正確全名http://rpm.pbone.net,例如 ncompress-0:4.2.4.4-3.fc21.src 將其更改為 ncompress-4.2.4.4-3.fc21.src.rpmdnf下載原始碼時可開啟鏡像偵錯,以防鏡像伺服器宕機。看這。
[更新]
如果您像我一樣因為無效的儲存庫而出現 rpm 錯誤,這就是我修復它的方法:
$ sudo dnf config-manager --set-enabled '*' #Enable all repos, at anytime, check with `sudo dnf repolist all`
$ repoquery --resolve --archlist=src '*compress*'
Could not match packages: failure: repodata/repomd.xml from rpmfusion-free-rawhide-source: [Errno 256] No more mirrors to try.
http://free.nchc.org.tw/rpmfusion/free/fedora/development/rawhide/source/SRPMS/repodata/repomd.xml: [Errno 14] HTTP Error 404 - Not Found
...
$ repoquery --resolve --archlist=src --enablerepo='*source' --disablerepo='rpmfusion-free-rawhide-source' '*compress*' #not works too
...
$ sudo yum-config-manager --save --disablerepo=rpmfusion-nonfree-rawhide-source #for unknown reason, it doesn't work
$ sudo dnf config-manager --set-disabled rpmfusion-free-rawhide-source #for unknown reason, it doesn't work
$ grep -rnIH -D skip --color=always rpmfusion-free-rawhide-source /etc/yum.repos.d/
/etc/yum.repos.d/rpmfusion-free-rawhide.repo:17:[rpmfusion-free-rawhide-source]
$ sudo vi /etc/yum.repos.d/rpmfusion-free-rawhide.repo #Edit rpmfusion-free-rawhide-source from enabled=1 to enabled=0
$ repoquery --resolve --archlist=src '*compress*'#now should works :) repeat the `grep and vi` steps above if got error in other repos, in my case i have to disable rpmfusion-nonfree-rawhide-source too.
p/s:將標題從 [rpmfusion-free-rawhide-source] 編輯為 [rpmfusion-free-rawhide-source停用] hack 應該使 --enablerepo='*source' 有效,儘管到目前為止我發現這是不必要的,因為我已經在第一個命令中啟用了所有存儲庫。
答案3
我不知道這是否是一個答案,因為 ls 是用 C 編寫的,但您可以編寫一個 shell 腳本來使用 for 循環執行“ls”:
for f in *;do echo $f;完畢
它在一些靜態 shell 中也很有用...