tags:

views:

78

answers:

2

I have the following script

#!/usr/bin/Rscript

print ("shebang works")

in a file called shebang.r. When I run it from command line using Rscript it works

$ Rscript shebang.r

but when I run it from the command line alone

$ shebang.r

It doesn't work. shebang.r command not found.

If I type (based on other examples I've seen)

$ ./shebang.r

I get permission denied.

Yes, Rscript is located in /usr/bin directory

+4  A: 

Make the file executable.

chmod 755 shebang.r
Sjoerd
It worked! Are there other options besides 755 that work? I'm planning to sharpie the command on my laptop. Thanks.
Milktrader
@Milktrader: what is necessary is that the person trying to run the file have execute permission on it. Read the man page for `chown` and the section of the `ls` man page on the "Long format" (i.e. `ls -l`).
dmckee
Anything that sets the executable bit for the owner should work, as long as you are the owner of the file. Minimally, you need `100`, but then you won't be able to read or write it anymore. You probably want at least `700`, which gives the owner read, write and execute permissions, but denies all permissions to everyone else (except root).
Thomas
See also http://www.zzee.com/solutions/unix-permissions.shtml
Thomas
thanks all for the chmod MAN page reference. It has all the info I need
Milktrader
or `chmod +x shebang.r` it's easier to remember that +x stands for eXecutable...
aL3xa
+1  A: 

In addition to Sjoerd's answer... Only the directories listed in the environment variable PATH are inspected for commands to run. You need to type ./shebang.r (as opposed to just shebang.r) if the current directory, known as ., is not in your PATH.

To inspect PATH, type

echo $PATH

To add . to PATH, type

export PATH="$PATH:."

You can add this line to your ~/.bashrc to make it happen automatically if you open a new shell.

Thomas
yes, I do need to use ./shebang.r to get it to work. I was told that adding . to the PATH variable introduces security risk, but I might do it anyway since it's my personal computer and it's not like I have missile launch codes on it.
Milktrader
The security risk is that you might be in a directory where someone else put, say, a program named `ls` that you might accidentally execute. If `.` is last in your `PATH`, the risk is pretty small, because the usual `/bin/ls` will take precedence, and `.` will only be searched in the case of a nonexistent command. Still, I'd recommend against adding `.` to the `PATH` of the root user, because the potential damage is greater. See further https://listman.redhat.com/archives/redhat-list/1999-July/msg01969.html
Thomas