help please!!!!

how do you get the file permissions to strings
like rwx------ read,write,executable to users

Rather than going for a script, see if the command aclget solves your purpose.

man aclget

check if this is what you want:

stat -c '%A' filename

or

stat -c '%a' filename

Can the result from this be used as a variable..I mean if a file has following permissions
rwxr-xr--
I want the output to be:
this file has the following permissions:-
Read, Write and Execute for the user.
Read and Execute for the group.
Read only for the others.

Is it possible?

maybe something like :-

filename=$1
uperms=`ls -l $filename|cut -c2-4`
gperms=`ls -l $filename|cut -c5-7`
operms=`ls -l $filename|cut -c8-10`

then you can case for the responses you want or whatever.

Or even just run the command once.

perms=`ls -l "$1" | cut -f1`
case $perms in
  ?r?????????) echo Read for owner;;
esac
case $perms in
 ??w???????) echo Write for owner;;
esac

etcetera. For maintainability, it would be better to loop over user, group, and other, and only case on the first three characters of the value.

perms=${perms#?}
for role in user group other; do
  case $perms in
    r??*) echo read for $role;;
  esac
  case $perms in
    ?w?*) echo write for $role;;
  esac
  case $perms in
    ??x*) echo execute for $role;;
  esac
  perms=${perms#???}
done
1 Like

thanx a lot era and repudi8or..but i ve got a problem in case..
will i be able to get to conditions in one line..
like if a file has rx for the group
i want to have the out put to be read, execute to the user

i know when you put in the case
r?x) echo read execute to the user
will do but it also gives the individual out puts..how do you stop that ???

You can collect them into a single string instead of printing one line for each.

perms=`ls -l "$1" | cut -f1`
perms=${perms#?}
for role in user group other; do
  what=none
  case $perms in
    r??*) what=read;;
  esac
  case $perms in
    ?w?*) what="$what"${what:+", "}"write";;
  esac
  case $perms in
    ??x*) what="$what"${what:+", "}"execute";;
  esac
  echo "$what for $role"
  perms=${perms#???}
done

The construct ${variable:+something} means "if variable is empty, substitute nothing; otherwise, substitute 'something'". Your shell might be picky about how much quoting it tolerates in this construct; play around with different quoting if you can't get this to work directly (or simply give up on having commas between the values, as a crude workaround).