Operating System - HP-UX
1753776 Members
7313 Online
108799 Solutions
New Discussion юеВ

Question about execute and read bits

 
SOLVED
Go to solution
David Yandry
Frequent Advisor

Question about execute and read bits

Hi,

I'm studying for the CSA exam and I have a question. Do both the read bit and the execute bit have to be set so that the file can be executed by a regular user? My study materials say yes but I thought I remembered seeing a thread that said that was not true.

Does anyone remember that thread or know the answer for sure?


TIA,
David
6 REPLIES 6
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Question about execute and read bits

Well David, this is a tough question not because of the question itself. The correct answer is a resounding "NO" -- only the execute bit must be set BUT that may not be the correct test answer. If the question, for example, pertains to a shell script then the read bit is also necessary but in the strictest sense, a shell script is not an executable because it relies upon an underlying true executable (the shell) to actually execute.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Question about execute and read bits

Oh, and this is one of those questions that you should ask the box itself; on account of, I might lie to you and whatever study guides you may be using may lie to you but the box don't lie. Try this:

Create a small file, hello.c:
----------------------------------
int main()
{
(void) printf("Hello World!\n");
return(0);
}
-----------------------------------
Now compile it:
cc hello.c -o hello

Now, let's set ONLY the execute for the file owner (presumably you):
chmod 100 hello

Now let's try to execute it:
hello

If you see "Hello World!" then only the execute is necessary; otherwise you also need the read bit.

Q.E.D.

Sadly, however, this may or may not tell you what is the "correct" test answer.
If it ain't broke, I can fix that.
Roboz
Frequent Advisor

Re: Question about execute and read bits

Hello David,
I believe that Clay is correct in the execute bit for a executable binary but keep in mind that a file needs to have its read bit set in order to be open and its content examined by the O/S. Thus a binary (compiled object) needs the read bit and the execute bit set. Shell script in the other hand do not require the execute bit as Clay correctly mentioned. However the read bit again is required by the shell to be able to read and interpret the command contained in the file.
Having gone through the CSA examination I believe that they are asking about shell scripts but.. hey the answer depends on the actual question you will be presented with.
I hope this is of assistance... good luck with the examination.
RV
A. Clay Stephenson
Acclaimed Contributor

Re: Question about execute and read bits

Actually, the shell script only requires the read bit because it is a data file. For example,"sh myfile" will execute the shell and treat myfile as data so ONLY the read bit needs to be set it that case. However, if both the read and execute are set then one can simply "execute" myfile and the script will run. I use "execute" in quotes in this context because again, the shell is the real executable and "myfile" is data.

An interesting aside is that if the exec() system call sees the magic number 043041 (octal) as the 1st two bytes of an executable file (#!) then it expects to see an executable follow and that is why, for example, if the first line of a script looks like:
#! /usr/bin/perl
or
#! /usr/bin/sh
then exec() starts /usr/bin/perl or
#! /usr/bin/sh as the actual executable and treats the remainder of the file as data for the executable. --- and that is how the "shebang" works.

Nevertheless, for the question as originally stated the correct answer is only the execute bit need be set --- but that may be wrong answer for the test -- but that's your problem, not mine.
If it ain't broke, I can fix that.
David Yandry
Frequent Advisor

Re: Question about execute and read bits

Hi Clay,

I compiled your Hello World program and set only the execute bit. I did see "Hello World!" showing that only the execute bit is needed.

Thanks,
David
A. Clay Stephenson
Acclaimed Contributor

Re: Question about execute and read bits

Quod Erat Demonstrandum.

Good for you. Now you know the correct answer it just may not be the "right" answer --- but that's a personal problem.
If it ain't broke, I can fix that.