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
24tmode="$1"
25
26typeset -i omode sbits
27typeset pmode
28
29# check for set-uid, etc. bits
30sbits=0
31case $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
35esac
36
37omode=0
38while :
39do
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
49done
50
51(( omode += sbits ))
52
53printf "0%o\n" $omode
54