Gather File permission during scripting on unix as numbers.

Hi,

I have a script with following file permission on box.
-rwxr-xr-x 1 root system 15347 Aug 14 15:08 b_reboot.ksh

Without calculating or watching at -rwxr-xr-x (permission's) of this above mentioned file. I would like to get the file permission assigned to a file.

Basically looking for after issuing some commands <file name>, I should get the permission of provided file in a variable as 755. (as a number, either 755/644..or what ever it is.)

Example,

$some_command <file_name>
$permission=$?

And the output which I looking forward is like this.
$print ${permission}
755

Do we have any command which can substitute "some_command" like this on Unix.?

Note:
------
I don't like to go for the manipulation using the ls -l |grep <filename> |awk '{print $1}'
Looking for any thing which can be easily done to retrieve permission of file in numbers.

do you mean set file permissions to 755?

chmod 755 filename

If you mean get the permissions, and you are in Linux check out the stat command.  That would be 
man 1 stat 
#or
stat --help 

from what I read, ajilesh is wanting to see what the permissions are in a non-volatile way. I don't know how you do this in csh, but in ksh you can do this:

if [[ -x /usr/lib/sendmail ]] this is true if the user doing the condition, can execute /usr/lib/sendmail
if [[ -r /usr/lib/sendmail ]] this is true if the user doing the condition, can read /usr/lib/sendmail
if [[ -w /usr/lib/sendmail ]] this is true if the user doing the condition, can write /usr/lib/sendmail

but this only checks the permissions of the user performing the, in this case, if/then/else has those permissions. I do not know of any way to convert rwxrw-rw- to the octal 755 to then confirm you have the correct permissions on said file or directory. I have run into this same problem in my own scripting because the file I was checking on needed to be 766 and I was trying to track down when the permissions got changed so I could figure out what was changing the permissions, the only option I found was to ls -l <file> then pipe that into a grep on "rwxrw\-rw\-" to confirm (yes, \- is necessary to ensure it doesn't try to treat -rw as an option some how) it had the correct permissions.

while using chmod is a good suggestion, chmod sets the permissions without regard to the previous state.

James, well, you are correct. I'm looking for any way to convert rwxrw-rw- to the octal 755.. Do we have any internal unix commands where we can trace the octal which is assigned to a file or directory directly.

find . -name "*" -maxdepth 1 -printf '%m %p\n'
or
stat -c "%a %n" *

both are not working in AIX..:confused:

If you have Perl, you can use that as a workaround.

perl -e 'for (@ARGV) { printf "%04o %s",  (stat)[2] & 07777, $_ }' files ...

oh great it worked...
is there any other command than stat command in unix??