ls コマンドのシェル スクリプトはどこにありますか?

ls コマンドのシェル スクリプトはどこにありますか?

コマンドがどのように動作するかを理解しようとしていますlsが、ファイルシステムのどこかに定義するシェル スクリプトがあると思います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

stdin をメモリにマップします。(理由は不明です。ソースを参照してください)

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シェル スクリプトではありませんが、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

注記:

  1. coreutils-8.22-22.fc21.src.rpm は私のものですが、パッケージ番号は異なる場合があります。

  2. type -a history「履歴はシェルの組み込みコマンドです」という戻り値のようなコマンドは、現在のシェルのソースコードを確認する必要があります。つまり、 rpm -qf `readlink -f /proc/$$/exe`(コマンドによって現在のシェルを検出すると、トリッキー思ったよりも、このトリックはfishシェルでは機能しません)

  3. csh/tcshシェルでは、where historyそのようなコマンドはないので、を使用する必要がありますtype。詳細については、ここ

  4. ワイルドカードを試すのも面白いかもしれません。例えば、repoquery --resolve --archlist=src '*compress*'インストールされていないパッケージを含めるなどです(「*uncompress*」のようなクエリコマンドの場合は注意してください。この場合、最初の試み「*uncompress*」が失敗した場合に絞り込むためにプレフィックス「un」を削除する必要があります)。repoquery上記の出力から中間の「un」を削除する必要があります。0:そしてオプションで.rpm正しいフルネームを取得するには、http://rpm.pbone.netたとえば、ncompress-0:4.2.4.4-3.fc21.src を ncompress-4.2.4.4-3.fc21.src.rpm に変更します。

  5. ミラーサーバーがダウンした場合に備えて、dnfダウンロードソース時にミラーデバッグを有効にすることができます。これ

[アップデート]

私のように無効なリポジトリが原因で 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.

追記:タイトルを[rpmfusion-free-rawhide-source]から[rpmfusion-free-rawhide-source]に編集します無効にする] ハックにより --enablerepo='*source' が機能するはずですが、これまでのところ、最初のコマンドですべてのリポジトリを既に有効にしているため、これは不要であることがわかりました。

答え3

ls は C で書かれているので、これが答えかどうかはわかりませんが、for ループを使用して「ls」を実行するシェル スクリプトを書くことができます。

for f in *;do echo $f; 完了

一部の静的シェルでも役立ちます...

関連情報