- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Getting file permissions in 'NNN' mode
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-12-2001 08:19 AM
тАО09-12-2001 08:19 AM
Getting file permissions in 'NNN' mode
I want to obtain a file's permission in 'NNN' mode, not
in '-rwxrwxrwx' mode.
I mean, I would like a script to get DIRECTLY '666' from
a file, not '-rw-rw-rw'.
Any help?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-12-2001 08:49 AM
тАО09-12-2001 08:49 AM
Re: Getting file permissions in 'NNN' mode
I am not aware of any way to directly obtain the permissions in "octal format"
One of the ways you can do is put in a case statement in your script and convert the permissions to octal format.
Eg:
perm=`ls -al file | awk '{print $1}'
case $perm in
-rwxrwxrwx)
PERM=777 ;;
-rw-rw-rw-)
PERM=666 ;;
esac
Include all the permissions in the case statement. I am sure there is an easier way, just can't think of one right now.
-Ramesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-12-2001 09:42 AM
тАО09-12-2001 09:42 AM
Re: Getting file permissions in 'NNN' mode
I assume you're trying to get these permissions so that you can set permissions on some other file via a script? If that's the case, you should be able to use the octal values directly, i.e. you should be able to do chmod u=rw,g=rw,o=rw file.
This could be done using cut and sed, i.e something like:
PERM="-rw-rw-rw-"
UPERM=$(echo $PERM|cut -c2-4|sed s/\-//g|sed s/s/xs/g)
GPERM=$(echo $PERM|cut -c5-7|sed s/\-//g|sed s/s/xs/g)
OPERM=$(echo $PERM|cut -c8-10|sed s/\-//g|sed s/s/xs/g)
chmod u=$UPERM,g=$GPERM,o=$OPERM file.
Hope this helps.
-Santosh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-12-2001 09:52 AM
тАО09-12-2001 09:52 AM
Re: Getting file permissions in 'NNN' mode
Here is a 30 second perl script to do what you want:
#!/usr/bin/perl
$i = 0;
while ($i <= $#ARGV)
{
($dev,$ino,$mode) = stat($ARGV[$i]);
printf("%s\t%03o\n",$ARGV[$i],$mode & 0777);
++$i;
}
After making it executable, run it like this:
omode.pl myfile1 myfile2 myfile3
to produce this output:
myfile1 750
myfile2 400
myfile3 664
That should do it, Clay
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-12-2001 10:45 AM
тАО09-12-2001 10:45 AM
Re: Getting file permissions in 'NNN' mode
Darrell
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-12-2001 01:55 PM
тАО09-12-2001 01:55 PM
Re: Getting file permissions in 'NNN' mode
Easy as pie; we just mask off 3 less bits and expand the output field to 4 octal digits:
#!/usr/bin/perl
$i = 0;
while ($i <= $#ARGV)
{
($dev,$ino,$mode) = stat($ARGV[$i]);
printf("%s\t%04o\n",$ARGV[$i],$mode & 07777);
++$i;
}
That should do it with the setuid, setgid, & sticky bits displayed in the leftmost digits.
Another variation on the theme is to not mask anything and expand the octal field to reveal file type (regular, directory, fifo, ...) as well.
#!/usr/bin/perl
$i = 0;
while ($i <= $#ARGV)
{
($dev,$ino,$mode) = stat($ARGV[$i]);
printf("%s\t%08o\n",$ARGV[$i],$mode);
++$i;
}
Regards, Clay
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-12-2001 07:04 PM
тАО09-12-2001 07:04 PM
Re: Getting file permissions in 'NNN' mode
Here's a shell script to return the octal numeric mode of the file passed to it. The script handles the setuid, setgid and sticky bits along with the standard permissions:
#!/usr/bin/sh
#
typeset -i PERMFCT=0 #...permission mode factor
typeset -i MISCFCT=0 #...miscellaneous mode factor
typeset -i BITSNBR=2 #...bit number (1-relative)
typeset -i BITSVAL=0 #...simple bit value
typeset -i OCTMODE=0 #...final, octal numeric mode
#
[ "$#" != 1 ] && { echo "Usage: $0 file"; exit 1 ; }
SMODE=`ls -l $1|awk '{print $1}`
#
while (( BITSNBR <= 10 ))
do
case `expr \( $BITSNBR - 2 \) / 3` in #...establish weighted values...
0 ) PERMFCT=100
MISCFCT=4000
;;
1 ) PERMFCT=10
MISCFCT=2000
;;
2 ) PERMFCT=1
;;
esac
#
case `expr substr $SMODE $BITSNBR 1` in
r ) BITSVAL=4
MISCFCT=0
;;
w ) BITSVAL=2
MISCFCT=0
;;
x ) BITSVAL=1
MISCFCT=0
;;
s|t )
BITSVAL=1
;;
S|T )
BITSVAL=0
;;
* ) BITSVAL=0
MISCFCT=0
;;
esac
#
OCTMODE=`expr $PERMFCT \* $BITSVAL + $MISCFCT + $OCTMODE`
#
let BITSNBR=$BITSNBR+1
done
#
echo "mode = $OCTMODE"
exit 0
#
#.jrf.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-12-2001 07:18 PM
тАО09-12-2001 07:18 PM
Re: Getting file permissions in 'NNN' mode
>>> Corrected Copy <<<
----------------------
Here's a shell script to return the octal numeric mode of the file passed to it. The script handles the setuid, setgid and sticky bits along with the standard permissions:
#!/usr/bin/sh
#
typeset -i PERMFCT=0 #...permission mode factor
typeset -i MISCFCT=0 #...miscellaneous mode factor
typeset -i BITSNBR=2 #...bit number (1-relative)
typeset -i BITSVAL=0 #...simple bit value
typeset -i OCTMODE=0 #...final, octal numeric mode
#
[ "$#" != 1 ] && { echo "Usage: $0 file"; exit 1 ; }
#
SMODE=`ls -l $1|awk '{print $1}`
#
while (( BITSNBR <= 10 ))
do
case `expr \( $BITSNBR - 2 \) / 3` in #...establish weighted values...
0 ) PERMFCT=100
MISCFCT=4000
;;
1 ) PERMFCT=10
MISCFCT=2000
;;
2 ) PERMFCT=1
MISCFCT=1000
;;
esac
#
case `expr substr $SMODE $BITSNBR 1` in
r ) BITSVAL=4
MISCFCT=0
;;
w ) BITSVAL=2
MISCFCT=0
;;
x ) BITSVAL=1
MISCFCT=0
;;
s|t )
BITSVAL=1
;;
S|T )
BITSVAL=0
;;
* ) BITSVAL=0
MISCFCT=0
;;
esac
#
OCTMODE=`expr $PERMFCT \* $BITSVAL + $MISCFCT + $OCTMODE`
#
let BITSNBR=$BITSNBR+1
done
#
echo "mode = $OCTMODE"
exit 0
#
#.jrf.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-13-2001 01:31 AM
тАО09-13-2001 01:31 AM
Re: Getting file permissions in 'NNN' mode
echo local/bin/procmail | cpio -oB | cpio -iBtv
To get only the last three mode 'digits' (and no "... blocks" messages):
echo file | cpio -oB 2> /dev/null | cpio -iBtv 2>/dev/null | cut -c4-6
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-13-2001 02:14 AM
тАО09-13-2001 02:14 AM
Re: Getting file permissions in 'NNN' mode
Here's an awk way of doing it for 000-777 modes:
#!/bin/sh
ls -l $* | awk '
{
x=0
for (i=2;i<11;i++){
n=int((10-i)/3)
if (substr($1,i,1) == "r")
x=x+10**n*4
if (substr($1,i,1) == "w")
x=x+10**n*2
if (substr($1,i,1) == "x")
x=x+10**n*1
}
print x,$NF
}'
Rgds, Robin