Mohammedz.com

For Linux and Shell scripting.

Execution permission to scripts…why and why not???

Leave a comment


I had some friends asking on execution permission to scripts. As we all know, there are two ways to run a script.

1) /bin/bash program.sh
2) ./program.sh

Suppose, you have a new script program.sh WITHOUT execution permission. You can run that script using the first method </bin/bash program.sh>, but you will get permission denied error if you try with second method <./program.sh>. Why???

When you run the script like “/bin/bash program.sh”, the shell is
executing “program.sh” as an argument to “bash” program. Since “bash” is executable, the program can run even without an execution permission to program.sh.

When you run the script like “./program.sh”, the shell loads “program.sh” to the program area for execution. Since program.sh doesn’t have execution permission, it gives “Permission denied” error.

You can get the details by tracing the commands with strace (I’m pasting only the first line of strace output):

t.sh without execution permission
—————
~]$ strace ./t.sh
execve(“./t.sh”, [“./t.sh”], [/* 50 vars */]) = -1 EACCES (Permission denied)
—————

t.sh without execution permission
—————
~]$ strace bash t.sh
execve(“/bin/bash”, [“bash”, “t.sh”], [/* 50 vars */]) = 0
—————

t.sh with execution permission
—————
~]$ strace ./t.sh
execve(“./t.sh”, [“./t.sh”], [/* 50 vars */]) = 0
—————

See this part from execve manpage:
—————

NAME
execve – execute program

SYNOPSIS
#include <unistd.h>

int execve(const char *filename, char *const argv[], char *const
envp[]);

DESCRIPTION

execve() executes the program pointed to by filename. filename must be either a binary executable, or a script starting with a line of the form “#! interpreter [arg]”. In the latter case, the interpreter must be a valid pathname for an executable which is not itself a script, which will be invoked as interpreter [arg] filename.
—————

See “man execve” for more details.

~mohammed

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s