3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Eshell の Shebang で複数引数をサポートする

Posted at

要約

Eshell は

# !/usr/bin/env ruby -Ku

みたいな複数引数 Shebang をサポートするべき.

前置き

皆さん Eshell 使ってますか?
Eshell は Emacs Lisp で書かれたシェルです.
当然 Emacs Lisp で拡張できるので色々やりたい放題できます.

それはともかく Shebang の話をします.
Shebang というのはシェルスクリプト等の先頭行に現れる,

foo.sh
# !/bin/sh

のように書かれている行のことで,これが付いているプログラムは,

$ ./foo.sh

とすれば /bin/sh foo.sh を実行した時と同じ結果を得られます.

しかしこの Shebang,複数の引数を指定した時の挙動は環境によるらしいです(Wikipedia の Shebangの項参照).
例えば以下のような Shebang だと(TOBY SOFT wiki から引用),

buzz.rb
# !/usr/bin/env ruby -Ku
$ ./buzz.rb
/usr/bin/env: ruby -Ku: そのようなファイルやディレクトリはありません

と Linux では言われます. Eshell でも同じ事を言われます.
Mac は複数引数の Shebang をサポートしているため,上記の Shebang を持つスクリプトも実行してくれます.

Mac のシェル上で動くのに Emacs のシェル上で動かないのはおかしいですよね?

なので Eshell に複数引数の Shebang サポートを追加しました.

本編

以下のコードを .emacs に追加してください.

.emacs
(defadvice eshell-script-interpreter (around esi activate)
  (setq ad-return-value
        (let ((file (ad-get-arg 0))
              (maxlen eshell-command-interpreter-max-length))
          (if (and (file-readable-p file)
                   (file-regular-p file))
              (with-temp-buffer
                (insert-file-contents-literally file nil 0 maxlen)
                (when (re-search-forward "^#![ \t]*\\(.+\\)$" nil t)
                  `(,@(split-string (match-string 1)) ,file)))))))

それでは,よき Emacs & Eshell ライフを!

Q: 引数を複数 Shebang 内に書くのって全然ポータブルな書き方じゃないよね.

  • Eshell で複数引数 Shebang が動く
  • Eshell は Emacs 上で動く
  • Emacs はどこでも動く

良かったですね.どの環境でもちゃんと動きます.ポータブルです.

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?