Permissions

Hi guys,

i write the below script to make the user get to the directory that interesting. Now what I am trying is to check the permissions of the directory and if the directory exists to check the reading options.

echo "Please enter your desire folder directory ( \yourfolders) ?: \c"    
            read yourfolder
            echo
            # **** Check if the directory exists ******************
            # **** find out if file has write permission or not ***
            # *****************************************************
            
            [ -w $yourfolder ] && W="Write = yes" || W="Write = No"
            
            # *****************************************************
            # *** find out if file has excute permission or not ***
            # *****************************************************
            [ -x $yourfolder ] && X="Execute = yes" || X="Execute = No"
            
            # *****************************************************
            # ****  find out if file has read permission or not ***
            # *****************************************************
            [ -r $yourfolder ] && R="Read = yes" || R="Read = No"
 
                echo "$yourfolder Permissions"
                echo "$W"
                echo "$R"
                echo "$X"
            if 
            [! -d "$yourfolder" ]; then
                echo "That directory exists and below you can see the directory and files: \c"
                echo
                cd $yourfolder
                echo
                ls -ltr
                echo
                pwd
                echo "Thank you very much!!!"
            
            elif
            [ -r "$yourfolder" ]; then
                echo "That directory exists but not available for reading"
                exit
        
            # **** if the directory doesn't exists ****    
            else
                echo "That directory doesn't exists !!!"
                    
                echo "Thank you very much" 
                fi
                exit;;

Now as you can see I check the permissions but I don't know how to make the if statement. The first if statement it provide you the directory asap. In the second if checks if the directory exists. But for the permissions, HOUSTON we have a problem. I need to provide me a message that the folder is not accessible because of the permission " that " Any ideas?

An expression is 'invalid':

[! -d "$yourfolder" ]; then
                echo "That directory exists and below you can see the directory and files: \c"
                echo

Just put a spacechar between [ and !.

Further what confuses me is:

[ -r $yourfolder ] && R="Read = yes" || R="Read = No"

But then you say...

 [ -r "$yourfolder" ]; then
                echo "That directory exists but not available for reading"
                exit
        

hth

EDIT:
Maybe this helps

for V in r w x d;do X="[ -$V $yourfolder ]"; $X && RET=yes||RET=no; echo "$V:$RET";done

More readable :

for V in r w x d;do
	X="[ -$V $yourfolder ]"
	$X && \
		RET=yes || \
		RET=no
	echo "$V:$RET"
done

What you're doing with X is a very bad idea. The value of $yourfolder will be subject to field splitting and pathname expansion after the unquoted expansion of $X. This could cause testing of an unintended pathname, or a syntax error in the resulting test/[ command.

Regards,
Alister

Cannot run it!!!

---------- Post updated at 09:34 PM ---------- Previous update was at 09:33 PM ----------

I am thinking if I have to create case that check first the reading permissions and then if statement to make the results.

True.

[sea@dell ~]$ yourfolder=/root
[sea@dell ~]$ for V in r w x d;do X="-$V";[ $X "$yourfolder" ] && RET=yes||RET=no; echo "$V:$RET";done
r:no
w:no
x:no
d:yes

Would be better..

@ mikerousse: Whats not working?
EDIT: Yes that is a good idea.
Why not make the output at the same time you're checking the file?
Or, append it (the message [file not available etc], not the info [read = xy]) to an output variable and print that at the end?

I would like to thank you for your help guys but I find at last a solution.

Then, for the future benefit of anyone having a similar problem, please post your solution.

Regards,
Alister

---------- Post updated at 06:50 PM ---------- Previous update was at 06:41 PM ----------

Definitely better, although you can go a bit further and discard X altogether.

Regards,
Alister

@ Alister: I did have it without '$X', but then it failed upon missing argument...
@ mikerousse :

'That' is not a regular permission :wink:

You are / were checking both times for the same result, but YOUR message 'That directoryy exists...' was misleading, as it is only printed when user CAN READ it.
But it should only print that message when it cant read it.
[ ! -r $yourfolder ] && echo "Cannot access $yourfolder!" && exit 1

EDIT:

            elif
            [ ! -r "$yourfolder" ]; then
                echo "That directory exists but not available for reading"
                exit 1 # If this is a 'failexit', use 'exit 1', otherwise, leave as is/was
        
            # **** if the directory doesn't exists ****    
            else

hth

It was very simple, I just make the following if statement:

if [ -r "$yourfolder" ] && [ -d "$yourfolder" ]; then .....

Perhaps there was a typo in your code? The following works fine:

$ for V in r w x d; do [ -$V filename ] && RET=yes || RET=no; echo $V:$RET; done
r:yes
w:yes
x:no
d:no

Regards,
Alister