home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / unix / shell / 3647 < prev    next >
Encoding:
Text File  |  1992-08-26  |  4.1 KB  |  142 lines

  1. Path: sparky!uunet!dtix!darwin.sura.net!jvnc.net!netnews.upenn.edu!midnight.seas.upenn.edu!chip
  2. From: chip@midnight.seas.upenn.edu (Charles H. Buchholtz)
  3. Newsgroups: comp.unix.shell
  4. Subject: Re: Help with directories!
  5. Message-ID: <87121@netnews.upenn.edu>
  6. Date: 26 Aug 92 20:25:05 GMT
  7. References: <1992Aug26.180859.22212@netnews.synoptics.com>
  8. Sender: news@netnews.upenn.edu
  9. Organization: University of Pennsylvania
  10. Lines: 129
  11. Nntp-Posting-Host: midnight.seas.upenn.edu
  12.  
  13. ericd@synoptics.com writes:
  14. >Charles H. Buchholtz writes:
  15. >>
  16. >>By the way, as I recall from the discussion earlier this month in this
  17. >>news group, there are three approaches to getting octal permissions:
  18. >>C, perl, and awk.  No one reported an easy solution, such as an option
  19. >>to ls.
  20. >
  21. >I looked for one, but did not find one. If you know of one please offer it up!
  22.  
  23. For the record: it took me about 2 minutes to find these articles.
  24. The question was reposted five days after the solution was posted.  At
  25. my site, there were only 50 articles between the last solution and the
  26. repost of the question.  We're not talking about poring through
  27. ancient archives here.
  28.  
  29. I am posting as an individual, not as a representative of U. of P.
  30.  
  31. Charles H. Buchholtz       Systems Programmer     chip@seas.upenn.edu
  32.           School of Engineering and Applied Science
  33.               University of Pennsylvania
  34.  
  35.  
  36. -----------------------------------------------------------------------
  37. From: hsc@sybase.com (Howard Cohen)
  38. Newsgroups: comp.unix.shell
  39. Subject: Re: Octal chmod status
  40. Date: 14 Aug 92 00:30:18 GMT
  41.  
  42. #!/bin/sh
  43.  
  44. LSPERMS=`ls -l "$1" | sed -e "s:^\([^  ][^     ]*\).*:\1:"`
  45. USR=`echo $LSPERMS X| sed -e "s:.\(...\).*:\1:"`
  46. GRP=`echo $LSPERMS X| sed -e "s:....\(...\).*:\1:"`
  47. OTH=`echo $LSPERMS X| sed -e "s:.......\(...\).*:\1:"`
  48.  
  49. PERMS=""
  50. for elem in $USR $GRP $OTH
  51. do
  52.      case "$elem" in
  53.          ---)    PERMS="${PERMS}0";;
  54.          --x)    PERMS="${PERMS}1";;
  55.          -w-)    PERMS="${PERMS}2";;
  56.          -wx)    PERMS="${PERMS}3";;
  57.          r--)    PERMS="${PERMS}4";;
  58.          r-x)    PERMS="${PERMS}5";;
  59.          rw-)    PERMS="${PERMS}6";;
  60.          rwx)    PERMS="${PERMS}7";;
  61.      esac
  62. done
  63.  
  64. echo $PERMS
  65.  
  66. -----------------------------------------------------------------------
  67. From: mju@mudos.ann-arbor.mi.us (Marc Unangst)
  68. Newsgroups: comp.unix.shell
  69. Subject: Re: Octal chmod status
  70. Date: 15 Aug 92 05:11:08 GMT
  71.  
  72. #include <stdio.h>
  73. #include <sys/types.h>
  74. #include <sys/stat.h>
  75. #include <errno.h>
  76.  
  77. int
  78. main(int argc, char **argv)
  79. {
  80.   struct stat statbuf;
  81.  
  82.   if(argc != 2) {
  83.     fprintf(stderr, "%s: usage: %s file\n", argv[0], argv[0]);
  84.     exit(1);
  85.   }
  86.  
  87.   if(stat(argv[1], &statbuf) != 0) {
  88.     fprintf(stderr, "%s: can't stat file %s: %s\n", argv[0], argv[1],
  89.             strerror(errno));
  90.     exit(1);
  91.   }
  92.  
  93.   printf("%o\n", statbuf.st_mode & 07777);
  94.   exit(0);
  95. }
  96. -----------------------------------------------------------------------
  97. From: tchrist@convex.COM (Tom Christiansen)
  98. Newsgroups: comp.unix.shell
  99. Subject: Re: Octal chmod status
  100. Date: 18 Aug 92 18:16:09 GMT
  101.  
  102.     perl -e 'printf("%04o\n", (stat($ARGV[0]))[2] & 07777)'
  103.  
  104. -----------------------------------------------------------------------
  105. From: tchrist@convex.COM (Tom Christiansen)
  106. Newsgroups: comp.unix.shell
  107. Subject: Re: Octal chmod status
  108. Date: 19 Aug 92 14:30:59 GMT
  109.  
  110.     [perl solution, optimized for readability ---Chip]
  111.  
  112.     require 'stat.pl';
  113.     @sb = stat($ARGV[0]);
  114.     printf("%04o\n", $sb[$ST_MODE]);
  115.  
  116. -----------------------------------------------------------------------
  117. From: rouben@math13.math.umbc.edu (Rouben Rostamian)
  118. Newsgroups: comp.unix.shell
  119. Subject: Re: Help with directories!
  120. Date: 26 Aug 92 14:50:29 GMT
  121.  
  122. Create a file called convert.awk, containing:
  123.  
  124. {       for (i=2; i<=10; i++) {
  125.                 n = 2*n
  126.                 if ( substr($0,i,1) != "-" )
  127.                         n++;
  128.         }
  129.         printf ("%o\n", n)
  130. }
  131.  
  132. Then the apply the command:
  133. echo -rwxr-xr-x | awk -f convert.awk
  134. to get 755.
  135.  
  136. If you have nawk, you can write the script a bit more elegantly as:
  137.  
  138. {       for (i=2; i<=10; i++)
  139.                 n = 2*n + ( substr($0,i,1) != "-" ? 1 : 0 )
  140.         printf ("%o\n", n)
  141. }
  142.