我正在尝试使用 Emacs Lisp 作为替代 shell 脚本语言,但遇到了一个奇怪的问题。我在名为 hello.el
的文件中编写了以下 Emacs Lisp 脚本,然后使其可执行:
#!/usr/local/bin/emacs --script
(princ "hello world\n")
当我运行它时,我得到以下结果:
$ ./hello.el
./hello.el: line 2: princ: command not found
经过一些实验,我发现 #!
下面的代码是由 Bash 或至少是其他一些 shell 运行的,而不是 Emacs:
#!/usr/local/bin/emacs --script
foo=bash?
echo $foo
ls -l | sort | tail -1
$ ./hello.el
bash?
total 7984
这对我来说似乎很奇怪,因为在各种 Emacs 文档中有很多对此 --script
选项的引用,当我直接使用它运行脚本时,它工作得很好(即当我将 /usr/local/bin/emacs --script hello.el
粘贴到我的终端中。
真正奇怪的是这个脚本完全按照预期工作:
#!/usr/bin/emacs --script
(princ "hello world\n")
但是我使用 /usr/local/bin/emacs
的原因是它是较新的版本; /usr/bin/emacs
中的版本是 Apple 提供的 Mac OS X 附带的 Emacs,而 /usr/local/bin/emacs
中的版本是通过brew 以及更新的版本(Emacs 24 与 22)。
这里出了什么问题? Emacs 22 和 24 之间有什么变化吗?
最佳答案
所以我写下了这个问题,并在意识到问题是什么之前了解了一些关于 Mac OS X 的知识,并认为我不妨在这里发布答案,因为它有点神秘。希望对您有所帮助。
问题是 /usr/local/bin/emacs
是一个符号链接(symbolic link),不是指向 Emacs 二进制文件,而是指向 shell 脚本:
$ ls -l /usr/local/bin/emacs
lrwxr-xr-x 1 aki staff 30 May 20 2013 /usr/local/bin/emacs -> ../Cellar/emacs/24.3/bin/emacs
$ file /usr/local/Cellar/emacs/24.3/bin/emacs
/usr/local/Cellar/emacs/24.3/bin/emacs: Bourne-Again shell script text executable
$ cat /usr/local/Cellar/emacs/24.3/bin/emacs
#!/bin/bash
/usr/local/Cellar/emacs/24.3/Emacs.app/Contents/MacOS/Emacs -nw "$@"
我不知道为什么#!/path/to/a/shell/script
无法“做正确的事”。我尝试过它,它似乎只是将您的第一个脚本作为 shell 脚本运行,而不是像您所期望的那样将脚本的名称作为参数传递给第一个脚本。也许某个 shell 向导可以对此发表评论?
与此同时,这有效:
#!/usr/local/Cellar/emacs/24.3/bin/emacs-24.3 --script
(princ "hello world\n")
因为这是正确的 Emacs 二进制文件的路径。我想你会想用 env
做一些废话或其他任何事情来使其可移植。
希望这可以帮助其他尝试在 Mac 上执行此操作的人。
关于macos - shebang emacs --脚本作为 bash 运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21957173/