如何使我的R脚本可执行?

k88*_*074 12 r shebang rscript

我知道这是一个很复杂的风险,但在这里没有其他问题我找到了我的问题的答案.以下是我已经尝试过的摘要.

我有一个R脚本文件file.r:

#!/usr/bin/env Rscript 
print("Hello World!")
Run Code Online (Sandbox Code Playgroud)

这是可执行的(chmod +x file.r),并且曾经很好地运行(上次我使用它大约一个月前),发出:

$ ./file.r
Run Code Online (Sandbox Code Playgroud)

但是,今天:

$ ./file.r
/usr/bin/env: 'Rscript\r': No such file or directory
Run Code Online (Sandbox Code Playgroud)

事实上:

$ which Rscript
/usr/bin/Rscript 
Run Code Online (Sandbox Code Playgroud)

因此,我将shebang改为:#!/usr/bin Rscript,但是:

$ ./file.r
/usr/bin: bad interpreter: Permission denied
Run Code Online (Sandbox Code Playgroud)

然后我想我会以超级用户身份运行它,但是:

$ sudo ./file.r
sudo: unable to execute ./file.r: Permission denied
Run Code Online (Sandbox Code Playgroud)

阅读我发现R的全新安装将解决我的问题,所以我卸载并安装了R.不幸的是我之前写的仍然适用.但请注意,以下适用于shebang版本:

$ Rscript file.r
[1] "Hello World!"
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Rav*_*avi 11

啊,它的回车(\ r)问题,它被添加到第一行,如果你使用的是vi编辑器,:set list它会显示它.行结尾将显示为$并且回车字符显示为^ M.

#!/usr/bin/env Rscript  Makes your script portable than #!/usr/bin/Rscript
Run Code Online (Sandbox Code Playgroud)

顺便说一下,你可以通过进入insert(i)/ Append(a)模式并在ctrl + v然后按ctrl + m进入vi