为什么使用 RScript 调用 R 脚本有效,但不适用于 R 二进制文件?

Why does invoking R script using RScript works but not with R binary?

关于 she-bang 的工作方式有什么我想念的吗?

# why does this not work?
§ ./sample.r 
./sample.r: line 3: 1: command not found

§ ll sample.r 
-rwxr-xr-x 1 raghu.dodda staff 39 Jun 14 19:10 sample.r

§ cat sample.r
#!/usr/local/bin/r --slave -f 

1 + 1

# this is what the above command should effectively be doing; no?
§ /usr/local/bin/r --slave -f sample.r  
[1] 2

我知道 运行 r 脚本的推荐方法是使用 Rscript,如果我改成那个,它就可以工作。但是,我很好奇(为了我自己的学习)为什么它不起作用。

§ cat sample.r
#!/usr/local/bin/Rscript --vanilla 

1 + 1

§ ./sample.r 
[1] 2

其他一些重要的事情:

(1) rR 都符号链接到同一个地方:

§ ls -l $(which r) $(which R)
lrwxr-xr-x 1 raghu.dodda admin 23 Jun 14 15:56 /usr/local/bin/R -> ../Cellar/r/4.0.1/bin/R
lrwxr-xr-x 1 raghu.dodda admin 23 Jun 14 15:56 /usr/local/bin/r -> ../Cellar/r/4.0.1/bin/R

(2) 我使用自制程序在 Mac 上安装了 r,即

brew install r

事实证明,由 /usr/local/bin/R/usr/local/bin/r 符号链接的文件是一个 shell 脚本 ,显然,在 macOS 上,这是 Wikipedia entry for she-bang.

的问题

In Darwin-based operating systems such as macOS, and in Solaris, the file specified by interpreter must be an executable binary, and cannot itself be a script.

§ realpath $(which r)
/usr/local/Cellar/r/4.0.1/bin/R

§ file /usr/local/Cellar/r/4.0.1/bin/R
/usr/local/Cellar/r/4.0.1/bin/R: POSIX shell script text executable, ASCII text

§ head -2 /usr/local/Cellar/r/4.0.1/bin/R
#!/bin/sh
# Shell wrapper for R executable.

然而,Rscript 是一个 二进制文件,因此它按预期工作:

§ which Rscript
/usr/local/bin/Rscript

§ file /usr/local/bin/Rscript
/usr/local/bin/Rscript: Mach-O 64-bit executable x86_64

在 Linux 上,即使 r 符号链接到脚本(不确定是否是这种情况),它也会按预期工作,基于此:

On Linux and Minix, an interpreter can also be a script. A chain of shebangs and wrappers yields a directly executable file that gets the encountered scripts as parameters in reverse order.