Patch-ID: bash40-021
[bash.git] / examples / scripts / showperm.bash
blob2e06c0bc6b77277ae93d48ee2679f552f578606d
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
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...
12 # Converted to Bash v2 syntax by Chet Ramey <chet@po.cwru.edu>
14 # usage: showperm modestring
16 # example: showperm '-rwsr-x--x'
19 [ -z "$1" ] && {
20 echo "showperm: usage: showperm modestring" >&2
21 exit 2
24 tmode="$1"
26 typeset -i omode sbits
27 typeset pmode
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
37 omode=0
38 while :
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 ))
48 esac
49 done
51 (( omode += sbits ))
53 printf "0%o\n" $omode