From: chip@midnight.seas.upenn.edu (Charles H. Buchholtz)
Newsgroups: comp.unix.shell
Subject: Re: Help with directories!
Date: 26 Aug 92 20:25:05 GMT
Organization: University of Pennsylvania

ericd@synoptics.com writes:
>Charles H. Buchholtz writes:
>>
>>By the way, as I recall from the discussion earlier this month in this
>>news group, there are three approaches to getting octal permissions:
>>C, perl, and awk.  No one reported an easy solution, such as an option
>>to ls.
>
>I looked for one, but did not find one. If you know of one please offer it up!

For the record: it took me about 2 minutes to find these articles.
The question was reposted five days after the solution was posted.  At
my site, there were only 50 articles between the last solution and the
repost of the question.  We're not talking about poring through
ancient archives here.

I am posting as an individual, not as a representative of U. of P.

Charles H. Buchholtz       Systems Programmer     chip@seas.upenn.edu
	      School of Engineering and Applied Science
		      University of Pennsylvania


-----------------------------------------------------------------------
From: hsc@sybase.com (Howard Cohen)
Newsgroups: comp.unix.shell
Subject: Re: Octal chmod status
Date: 14 Aug 92 00:30:18 GMT

#!/bin/sh

LSPERMS=`ls -l "$1" | sed -e "s:^\([^  ][^     ]*\).*:\1:"`
USR=`echo $LSPERMS X| sed -e "s:.\(...\).*:\1:"`
GRP=`echo $LSPERMS X| sed -e "s:....\(...\).*:\1:"`
OTH=`echo $LSPERMS X| sed -e "s:.......\(...\).*:\1:"`

PERMS=""
for elem in $USR $GRP $OTH
do
     case "$elem" in
         ---)    PERMS="${PERMS}0";;
         --x)    PERMS="${PERMS}1";;
         -w-)    PERMS="${PERMS}2";;
         -wx)    PERMS="${PERMS}3";;
         r--)    PERMS="${PERMS}4";;
         r-x)    PERMS="${PERMS}5";;
         rw-)    PERMS="${PERMS}6";;
         rwx)    PERMS="${PERMS}7";;
     esac
done

echo $PERMS

-----------------------------------------------------------------------
From: mju@mudos.ann-arbor.mi.us (Marc Unangst)
Newsgroups: comp.unix.shell
Subject: Re: Octal chmod status
Date: 15 Aug 92 05:11:08 GMT

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>

int
main(int argc, char **argv)
{
  struct stat statbuf;

  if(argc != 2) {
    fprintf(stderr, "%s: usage: %s file\n", argv[0], argv[0]);
    exit(1);
  }

  if(stat(argv[1], &statbuf) != 0) {
    fprintf(stderr, "%s: can't stat file %s: %s\n", argv[0], argv[1],
            strerror(errno));
    exit(1);
  }

  printf("%o\n", statbuf.st_mode & 07777);
  exit(0);
}
-----------------------------------------------------------------------
From: tchrist@convex.COM (Tom Christiansen)
Newsgroups: comp.unix.shell
Subject: Re: Octal chmod status
Date: 18 Aug 92 18:16:09 GMT

    perl -e 'printf("%04o\n", (stat($ARGV[0]))[2] & 07777)'

-----------------------------------------------------------------------
From: tchrist@convex.COM (Tom Christiansen)
Newsgroups: comp.unix.shell
Subject: Re: Octal chmod status
Date: 19 Aug 92 14:30:59 GMT

	[perl solution, optimized for readability ---Chip]

    require 'stat.pl';
    @sb = stat($ARGV[0]);
    printf("%04o\n", $sb[$ST_MODE]);

-----------------------------------------------------------------------
From: rouben@math13.math.umbc.edu (Rouben Rostamian)
Newsgroups: comp.unix.shell
Subject: Re: Help with directories!
Date: 26 Aug 92 14:50:29 GMT

Create a file called convert.awk, containing:

{       for (i=2; i<=10; i++) {
                n = 2*n
                if ( substr($0,i,1) != "-" )
                        n++;
        }
        printf ("%o\n", n)
}

Then the apply the command:
echo -rwxr-xr-x | awk -f convert.awk
to get 755.

If you have nawk, you can write the script a bit more elegantly as:

{       for (i=2; i<=10; i++)
                n = 2*n + ( substr($0,i,1) != "-" ? 1 : 0 )
        printf ("%o\n", n)
}