Operating System - HP-UX
1753500 Members
3682 Online
108794 Solutions
New Discussion юеВ

need help for shell script

 
Mikin Shah
New Member

need help for shell script

Dear Unix Expertise,

I need your help to write following shell script, as i am new to this area...

----------

Display the Octal value of Root Home Directory (700 / 750 / 777), User Home Directory and Application Home Directory...

For e.g. Result Should be like :

"Root Home Directory Octal Value : 700"

I want the OCTAL STATUS Output for each of these direcotries..

Pl. help me for this.
Thanks in Advance...

Regards,
Mikin
3 REPLIES 3
Dennis Handly
Acclaimed Contributor

Re: need help for shell script

You could just use something like the following. It doesn't handle sticky or setuid or setgid bits.
#!/usr/bin/ksh

#Display the Octal value of Root Home Directory (700 / 750 / 777),
#User Home Directory and Application Home Directory...

decode_3() {
octal=$1
r=${octal%??}
octal=${octal#?} # remove r
w=${octal%?}
x=${octal#?} # remove w
# echo "r:$r w:$w x:$x"
typeset -i total=0
if [ "$r" = "r" ]; then
(( total += 4 ))
fi
if [ "$w" = "w" ]; then
(( total += 2 ))
fi
if [ "$x" = "x" ]; then
(( total += 1 ))
fi
echo $total
}

decode_permission() {
perm=${1#?} # remove first
u_perm=${perm%??????}
perm=${perm#???} # remove user
g_perm=${perm%???}
o_perm=${perm#???} # remove group
# echo "u:$u_perm g:$g_perm o:$o_perm $2"
echo "$(decode_3 $u_perm)$(decode_3 $g_perm)$(decode_3 $o_perm) $2"
}

for file in ~root ~ $*; do
decode_permission $(ll -d $file | awk '{print $1, $9}')
done
James R. Ferguson
Acclaimed Contributor

Re: need help for shell script

Hi:

Use Perl;

# perl -le 'printf("%04o %-s\n",((stat($_))[2])&07777,$_) for @ARGV' file_or_directory

That is, pass one or more arguments for 'file_or_directory'. For example:

# perl -le 'printf("%04o %-s\n",((stat($_))[2])&07777,$_) for @ARGV' /var/tmp /home /etc/hosts
1777 /var/tmp
0755 /home
0644 /etc/hosts

Regards!

...JRF...
Pete Randall
Outstanding Contributor

Re: need help for shell script

Courtesy of Jack Mahaffey, here's "getchmod" (attached).


Pete

Pete