Check for file permission


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check for file permission
# 8  
Old 02-25-2014
SriniShoo's code helped !!

However, I need to know how would the same command work if the array contains both files and directories and I wish to check permissions for both.

I tried removing the type -f but that did not help.

Kindly suggest the changes.

---------- Post updated at 11:30 AM ---------- Previous update was at 10:43 AM ----------

Quote:
Originally Posted by bakunin
What do you mean by "lesser"? If i specify "660" and the file turns out to be "750", is that lesser, more or something entirely different? How about "755" or "555"?

Please clarify.

bakunin
660 is lesser than 750 becoz the [6]60 is less than [7]50.
755 is greater than 750 as all the permission are over met.
555 is lesser than 750 becoz [5]55 is less than [7]50.

Please let me know how can i tweak Srini's code to include directories along with files.
# 9  
Old 02-26-2014
Quote:
Originally Posted by SriniShoo
If the array has 10 elements, i.e., $arr[0] to $arr[9], below is the code

Code:
for i in $(seq 0 9);

Just use "${arr[@]}". You don't need to know how many elements exist nor do you need an external command like seq.


Quote:
Originally Posted by bakunin
You shouldn't do that. for-loops can break if you have too many elements. Use a while-loop instead:
I disagree. Any runtime that can't iterate over its own arrays is badly broken.

Regards,
Alister
# 10  
Old 02-26-2014
Thanks alister for the suggestion! you are correct about using
Code:
${arr[@]}

Below code will search for files / directories with permissions greater than or equal to given

Code:
PERM=750
for i in ${arr[@]}
do
ABS_FILE=$(echo ${i} | awk '{print $9}')
PATH=$(dirname ${ABS_FILE})
FILE=$(basename ${ABS_FILE})
find ${PATH} -name ${FILE} -perm -${PERM}
done

# 11  
Old 02-26-2014
Quote:
Originally Posted by SriniShoo
Code:
PERM=750
for i in ${arr[@]}
do
ABS_FILE=$(echo ${i} | awk '{print $9}')
PATH=$(dirname ${ABS_FILE})
FILE=$(basename ${ABS_FILE})
find ${PATH} -name ${FILE} -perm -${PERM}
done

If you give that as the path variable, it will overwrite the default path variable used by the system.

Last edited by chacko193; 02-26-2014 at 01:39 AM.. Reason: typo...
# 12  
Old 02-26-2014
My bad
# 13  
Old 02-26-2014
Quote:
Originally Posted by alister
[regarding for-loops that can break]
I disagree. Any runtime that can't iterate over its own arrays is badly broken.
The problem is not the iteration in itself. The problem is that expanding an array like "${array[*]}" might lead to a line that is longer than "MAXLINE" and/or consisting of more elements than "MAXARGS" allows for. You can experience the same phenomenon when using

Code:
for file in * ; do ...

Thi swill work well for any "normal" directory, but might break with a "line too long" if there are awful lots of entries in there. Instead of the for-loop the while-loop will not break:

Code:
ls | while read file ; do ...


I hope this helps.

bakunin
# 14  
Old 02-26-2014
Quote:
Originally Posted by bakunin
The problem is that expanding an array like "${array[*]}" might lead to a line that is longer than "MAXLINE" and/or consisting of more elements than "MAXARGS" allows for.
I'm assuming that by MAXLINE you mean LINE_MAX and that MAXARGS refers to ARG_MAX. In the case of expanding a for-loop's list, neither is relevant.

ARG_MAX limits the size of the command line and environment accepted by the execve system call. The for-loop does not execve the contents of the array; instead, it creates a list and consecutively binds each member to the same identifier.

LINE_MAX is the longest allowable line length (including the terminating newline character) in a text file. Expanding an array into a for-list using "${arr[@]}" does not generate a line or string. That double-quoted parameter expansion, like its positional parameter counterpart, "$@", directly generates a list.

I don't think it's germane, but while we're on the subject of LINE_MAX and sh, POSIX sh exempts shell scripts from this limit:
Quote:
INPUT FILES

The input file shall be a text file, except that line lengths shall be unlimited.
Historical shells may indeed have hardcoded limits, but I suspect that any shell that supports arrays is sufficiently modern to utilize dynamic memory allocation. Virtual memory is almost certainly the actual limit.

I am not advocating against a while-loop. I have nothing against them. Some of my closest friends are while-loops. My point is simply that categorically advising against a for-loop is a bit much; given a modern shell and sufficient memory, the likelihood of a for-loop meltdown is nil.

There also exists the (remote) possiblity that the for-loop mentioned in the OP is the C-like variant supported by ksh93 and bash, which would allow array traversal using a simple index.

Regards,
Alister

Last edited by alister; 02-26-2014 at 11:39 PM..
This User Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

File permission

Hi, Could you please tell me the OS command to grant as similar to below permission? rwxrwxr-x OS -- HP-UX Regards, Maddy (8 Replies)
Discussion started by: Maddy123
8 Replies

2. Shell Programming and Scripting

perl script to check read/write/execute permission for 'others'

I want to check access rights permissions not for 'user', not for 'group', but for 'others'. I want to do it by system command in which i want to use 'ls -l' and 'awk' command. I have written the following program : #!/usr/bin/local/perl #include <stdlib.h> system ("ls -l | awk... (1 Reply)
Discussion started by: shubhamsachdeva
1 Replies

3. Shell Programming and Scripting

shell script to check permission before copy over

Hi, I am writing some shell script to check the file owner permission whether the write bit is turn on. If it is turn on it will copy the file to the destination else it will just prompt user to change the file and skipping it. However, I am starting getting loss here as I have couple of... (2 Replies)
Discussion started by: wanaka
2 Replies

4. Shell Programming and Scripting

File permission check in SFTP mode

Hi, I have requirement to get a file from a remote location using SFTP only if the file has read/write permission either to the user or to the group. Currently i m taking the ls of the required file and storing it in a flat file to check the file permission. Please advice is there any... (3 Replies)
Discussion started by: ravin
3 Replies

5. UNIX for Advanced & Expert Users

Check EOF char in Unix. OR To check file has been received completely from a remote system

Advance Thanks. (1) I would like to know any unix/Linux command to check EOF char in a file. (2) Or Any way I can check a file has been reached completely at machine B from machine A. Note that machine A ftp/scp the file to machine B at unknown time. (5 Replies)
Discussion started by: alexalex1
5 Replies

6. Shell Programming and Scripting

Command to check the user's permission in UNIX ??

Hi, What is the command to check the user's permission in UNIX ? e.g: user in UNIX is "myuser" The result should be number format. e.g: 755, 644, etc... Thanks.. :) (3 Replies)
Discussion started by: suigion
3 Replies

7. Cybersecurity

file permission/acl: 2 users with write access on 1 file...

Hello, i need some help/advice on how to solve a particular problem. these are the users: |name | group | ---------- --------------- |boss | department1 | |assistant | department1 | |employee | department1 | |spy | department2 | this is the... (0 Replies)
Discussion started by: elzalem
0 Replies

8. Shell Programming and Scripting

sftp - permission setting check

Hello, I am doing a transfer of a file from one HP-UX ( umask : 033 ) server to another HP-UX server ( umask : 033 ) and file got transferred sucessfully. However I could see the file permission gets changed at the receiving end. Would like to know the reason for that? Details below: ... (3 Replies)
Discussion started by: jansat
3 Replies

9. UNIX for Dummies Questions & Answers

File Permission

Hi, When I listed one directory in Sun, it showed that : -rwsr-xr-x 1 root bsmbin 78004 Oct 21 2004 bsmprsm I don't know meaning of the character "s" in "rws" above. I have searched in Sun admin documents but no result. Would you please explain it ? :) Thank you so much. (1 Reply)
Discussion started by: msg098
1 Replies

10. UNIX for Dummies Questions & Answers

Script to check for a file, check for 2hrs. then quit

I wish to seach a Dir for a specific file, once the file is found i will perform additional logic. If the file is not found within two hours, i would like to exit. Logically, I'm looking for the best way to approach this Thanks for any assistance in advance. Note: I'm using a C shell and... (2 Replies)
Discussion started by: mmarsh
2 Replies
Login or Register to Ask a Question