How to make R script executable?

I know that this is associated with a high risk of being a duplicate, but in none of the other questions here I found an answer to my problem. Below is a summary of what I have already tried.

I have an R script file.r :

 #!/usr/bin/env Rscript print("Hello World!") 

which is executable ( chmod +x file.r ) and which was used to work beautifully (the last time I used it about a month ago) by issuing:

 $ ./file.r 

However today:

 $ ./file.r /usr/bin/env: 'Rscript\r': No such file or directory 

Actually:

 $ which Rscript /usr/bin/Rscript 

So I changed shebang to: #!/usr/bin Rscript , but:

 $ ./file.r /usr/bin: bad interpreter: Permission denied 

Then I thought that I would run it as root, but:

 $ sudo ./file.r sudo: unable to execute ./file.r: Permission denied 

Looking through, I found that a new installation of R would solve my problem, so I did not install and installed R. Unfortunately, everything I wrote is still applicable. Please note, however, that the following versions of both versions of shebang:

 $ Rscript file.r [1] "Hello World!" 

What am I doing wrong?

+5
source share
2 answers

Ah, its carriage return (\ r), it is added to the first line, if you use the vi editor :set list will show it. lines will be shown as $ and carriage return characters as ^ M.

 #!/usr/bin/env Rscript Makes your script portable than #!/usr/bin/Rscript 

Btw, you can insert \ r into vi by going into insert (i) / Append (a) mode and type ctrl + v and then ctrl + m

+3
source

If you want to point directly to the executable, you will need the full path after shebang (without space):

 #!/usr/bin/Rscript 

As Ravi noted, if this fix does not work, then the solution may simply include deleting the line and re-enabling.

I'm not a fan of the env workaround to make things more portable, because it makes the line more confusing, and most people don't realize that it actually calls another program (i.e. env ) to run the code in a modified shell. Read more about it here .

0
source

Source: https://habr.com/ru/post/1267103/


All Articles