Permissions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Permissions
# 1  
Old 03-25-2014
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.


Code:
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?

Last edited by mikerousse; 03-25-2014 at 03:11 PM.. Reason: code tags not ICODE!
# 2  
Old 03-25-2014
An expression is 'invalid':
Code:
[! -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:
Code:
[ -r $yourfolder ] && R="Read = yes" || R="Read = No"

But then you say...
Code:
 [ -r "$yourfolder" ]; then
                echo "That directory exists but not available for reading"
                exit

hth

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

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


Last edited by sea; 03-25-2014 at 03:55 PM..
# 3  
Old 03-25-2014
Quote:
Originally Posted by sea
Maybe this helps
Code:
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
# 4  
Old 03-25-2014
Quote:
Originally Posted by sea
An expression is 'invalid':
Code:
[! -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:
Code:
[ -r $yourfolder ] && R="Read = yes" || R="Read = No"

But then you say...
Code:
 [ -r "$yourfolder" ]; then
                echo "That directory exists but not available for reading"
                exit

hth

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

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

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.
# 5  
Old 03-25-2014
Quote:
Originally Posted by alister
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
True.
Code:
[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?

Last edited by sea; 03-25-2014 at 05:22 PM..
# 6  
Old 03-25-2014
I would like to thank you for your help guys but I find at last a solution.
# 7  
Old 03-25-2014
Quote:
Originally Posted by mikerousse
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 ----------

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

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

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Permissions

Hi, I have noticed that on my Linux box there is a nice feature which make it impossible for specified member (owner, group or other) to have an given access if a member from which we would expect it more don't have that access. So it is impossible to read file by all if others have set read... (3 Replies)
Discussion started by: DavidMax
3 Replies

2. Shell Programming and Scripting

ksh; Change file permissions, update file, change permissions back?

Hi, I am creating a ksh script to search for a string of text inside files within a directory tree. Some of these file are going to be read/execute only. I know to use chmod to change the permissions of the file, but I want to preserve the original permissions after writing to the file. How can I... (3 Replies)
Discussion started by: right_coaster
3 Replies

3. Shell Programming and Scripting

permissions

My /tmp is set with the following permissions (777) and a 't' at the end. My umask is set to 022. When I create a directory under /tmp (tmp/xx) it gets created as 755 as expected. Yet when I create a file within that directory (/tmp/xx/yy) the permissions are not 755 they are 644. ... (1 Reply)
Discussion started by: BeefStu
1 Replies

4. HP-UX

To give the "unzip" permissions & "create" file permissions

Hi, I am a Unix Admin. I have to give the permissions to a user for creating new file in a directory in HP-Ux 11.11 system since he cannot able to create a new file in the directory. Thanks in advance. Mike (3 Replies)
Discussion started by: Mike1234
3 Replies

5. UNIX for Dummies Questions & Answers

permissions

to prohibit 'others' from deleting files, what should we omit: write or execute? thx (9 Replies)
Discussion started by: melanie_pfefer
9 Replies

6. HP-UX

Permissions

Hey, We've got quite a strange problem on our hands here. We are running an HP 9000/800 B.11.00. I've just created a new group in /etc/group which i called, let's say newgroup . Then I added 4 users to the group, namely user1, user2, user3, user4 . The command grpchk shows no strange things... (4 Replies)
Discussion started by: chrizz
4 Replies

7. UNIX for Dummies Questions & Answers

permissions

I saved a perl code in xemacs. I used an xterminal to execute it but unix said that I don't have permission. I saved the files in my home directory. How do I change the permission. This is hat unix said: -ksh: ./names.pl: cannot execute (5 Replies)
Discussion started by: lnatz
5 Replies

8. UNIX for Dummies Questions & Answers

Permissions

Hi everyone, I'm looking for some information concerning Unix permissions. I am new to Unix and am doing research for a graduate class. Given the permissions below, can anyone give me five unique exploits that would be available to a hacker/cracker given this configuaration? -rw-rw-rw- 1... (1 Reply)
Discussion started by: skeeter
1 Replies

9. UNIX for Dummies Questions & Answers

help with permissions

Can anyone help explain the "s" in the below permissions example. I was reading about the "sticky bit" (t) but I am a little confused. On file "test" wolf% chmod 4777 test wolf% ls -l total 4 drwx------ 2 john staff 512 Mar 19 21:34 nsmail -rwsrwxrwx 1 john staff ... (2 Replies)
Discussion started by: finster
2 Replies

10. UNIX for Dummies Questions & Answers

permissions

with permission set to d-wx--x--x directoryname Why can't I do a long-listing on this directory? Is read access necessary? (1 Reply)
Discussion started by: mma_buc_98
1 Replies
Login or Register to Ask a Question