home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / unix / shell / 3633 < prev    next >
Encoding:
Internet Message Format  |  1992-08-26  |  1.7 KB

  1. Path: sparky!uunet!dtix!darwin.sura.net!haven.umd.edu!news.umbc.edu!math13.math.umbc.edu!rouben
  2. From: rouben@math13.math.umbc.edu (Rouben Rostamian)
  3. Newsgroups: comp.unix.shell
  4. Subject: Re: Help with directories!
  5. Message-ID: <1992Aug26.145029.19492@umbc3.umbc.edu>
  6. Date: 26 Aug 92 14:50:29 GMT
  7. References: <1992Aug24.175614.15627@netnews.synoptics.com> <1992Aug26.031742.12476@aplcen.apl.jhu.edu>
  8. Sender: newspost@umbc3.umbc.edu (News posting account)
  9. Organization: University of Maryland Baltimore Campus
  10. Lines: 43
  11.  
  12. In article <1992Aug26.031742.12476@aplcen.apl.jhu.edu> bandy@aplcen.apl.jhu.edu (Mike Bandy) writes:
  13. >>ericd@synoptics.com (Eric Davis) writes:
  14. >>
  15. >>I am looking for a sh routine to return a numerical value for a
  16. >>file/directory. i.e. the files permissions are -rwxr-xr-x it would
  17. >>return a 755.
  18. >>
  19. >>P.S. If there is a way to do it with an existing utility, please let me know.
  20.  
  21. >I've just read 18 messages regarding this question.  And not one of them
  22. >told us the answer (to which I'm curious or I wouldn't have wasted your
  23. >precious time with this message)...  Give us a break, and the answer to the
  24. >question, and let's get on with our lives ...
  25.  
  26. I agree.  Cut the religious arguments and offer solutions.  Here's how
  27. you do it in awk:
  28.  
  29. Create a file called convert.awk, containing:
  30.  
  31. {    for (i=2; i<=10; i++) {
  32.         n = 2*n
  33.         if ( substr($0,i,1) != "-" )
  34.             n++;
  35.     }
  36.     printf ("%o\n", n)
  37. }
  38.  
  39. Then the apply the command:
  40. echo -rwxr-xr-x | awk -f convert.awk
  41. to get 755.
  42.  
  43. If you have nawk, you can write the script a bit more elegantly as:
  44.  
  45. {    for (i=2; i<=10; i++)
  46.         n = 2*n + ( substr($0,i,1) != "-" ? 1 : 0 )
  47.     printf ("%o\n", n)
  48. }
  49.  
  50. Usage remains the same.  Handling suid bits, etc., is left as excercise.
  51. That's all, folks!
  52.  
  53. --
  54. Rouben Rostamian
  55.