Upload Tizen:Base source
[external/bash.git] / examples / scripts / showperm.bash
1 #Newsgroups: comp.unix.shell
2 #From: gwc@root.co.uk (Geoff Clare)
3 #Subject: Re: Determining permissions on a file
4 #Message-ID: <Dr79nw.DtL@root.co.uk>
5 #Date: Fri, 10 May 1996 17:23:56 GMT
6
7 #Here's a bit of Korn shell that converts the symbolic permissions produced
8 #by "ls -l" into octal, using only shell builtins.  How to create a script
9 #combining this with an "ls -l" is left as an exercise...
10 #
11 #
12 # Converted to Bash v2 syntax by Chet Ramey <chet@po.cwru.edu>
13 #
14 # usage: showperm modestring
15 #
16 # example: showperm '-rwsr-x--x'
17 #
18
19 [ -z "$1" ] && {
20         echo "showperm: usage: showperm modestring" >&2
21         exit 2
22 }
23
24 tmode="$1"
25
26 typeset -i omode sbits
27 typeset pmode
28
29 # check for set-uid, etc. bits
30 sbits=0
31 case $tmode in
32 ???[sS]*)       (( sbits += 8#4000 )) ;; # set-uid
33 ??????[sSl]*)   (( sbits += 8#2000 )) ;; # set-gid or mand. lock
34 ?????????[tT]*) (( sbits += 8#1000 )) ;; # sticky
35 esac
36
37 omode=0
38 while :
39 do
40         tmode=${tmode#?}
41         case $tmode in
42         "")       break ;;
43         [-STl]*)  (( omode *= 2 )) ;;
44         [rwxst]*) (( omode = omode*2 + 1 )) ;;
45         *)        echo "$0: first letter of \"$tmode\" is unrecognized" >&2
46                   (( omode *= 2 ))
47                   ;;
48         esac
49 done
50
51 (( omode += sbits ))
52
53 printf "0%o\n" $omode