test and if condition

Guys look at this:
i have to write a script that takes a file as an argument. The script should be able to determine what permissions the owner, group and everybody has for the file passed in. The output should be displayed similar to this.

READ WRITE EXECUTE
OWNER LEE.BALLANCORE YES YES NO
GROUP USERS YES NO NO
EVERYBODY NO NO NO

i ve been searching for the solution but i havent been able to assamble the notions...can anybody help me!!!!!!!!!!!!

this is what i saw aroand:

fperm=$(ls -l myfile|awk ' { print $1 } ')
echo $fperm
or=$(echo "$fperm"|cut -c2)
echo $or # owner read

or:

[clive@vle ~]$ ls -la .bashrc | cut -c1
-
[clive@vle ~]$ ls -la .bashrc | cut -c2
r
[clive@vle ~]$ ls -la .bashrc | cut -c3
w

This should get you started:

permreport()
{
  printf "%s\t" "$1"
  shift
  r=${1%??}
  wx=${1#?}
  w=${wx%?}
  x=${wx#?}
  for p in $r $w $x
  do
      case $p in
          -) printf "NO \t" ;;
          *) printf "YES\t" ;;
      esac
  done
  echo
}

FILE=$1
set -f
set -- $( ls -ld "$FILE" )

type_perms=$1
links=$2
owner=$3
group=$4
size=$5
month=$6
day=$7
time=$8 ## may be year
filename=$9 ## may include symlink target

perms=${type_perms#?}
filetype=${type_perms%"$perms"}
goperms=${perms#???}
uperms=${perms%"$goperms"}
gperms=${goperms%???}
operms=${goperms#???}

permreport OWNER $uperms
permreport GROUP $gperms
permreport WORLD $operms


Note that this doesn't take into account setuid and README bits.

THNX alot....u quite good with this staff!!!!!
thats way to much complicate for me...thare isn t another way to do it?
like using if statements and test?

There's nothing complicated in that script. Look at each statment and learn what it does. Read the shell man page, especially the Parameter Expansion section.

The only place where an if statement could be used is in place of case (and what's the point?); you would still need to break the output of ls into its components, and break up the permissions.

If your system has the stat command, that might make things a little easier.

Perhaps a teacher said to use those things in this assignment, maybe? :rolleyes:

Another way using tests

file=$1
set -- $(ls -ld $file)

perms=$1
owner=$3

[[ "$perms" = ?r???????? ]] && owner_read=YES
[[ "$perms" = ??w??????? ]] && owner_write=YES
[[ "$perms" = ???x?????? ]] && owner_exec=YES

[[ "$perms" = ????r????? ]] && group_read=YES
[[ "$perms" = ?????w???? ]] && group_write=YES
[[ "$perms" = ??????x??? ]] && group_exec=YES

[[ "$perms" = ???????r?? ]] && world_read=YES
[[ "$perms" = ????????w? ]] && world_write=YES
[[ "$perms" = ?????????x ]] && world_exec=YES

echo "perms: $perms"
echo "OWNER $owner ${owner_read:-NO} ${owner_write:-NO} ${owner_exec:-NO}"
echo "GROUP ${group_read:-NO} ${group_write:-NO} ${group_exec:-NO}"
echo "WORLD ${world_read:-NO} ${world_write:-NO} ${world_exec:-NO}"

I give you the same recommendation as cfajohnson :
Look at each statment and learn what it does. Read the shell man page.

THNX m8 thats very good code...i've gone throu`' it.
One prob...it's not reading the permission of any file i pass in the commands.

EG. sh file1 file2
i think it should read the permission of file 2 isn it?
but its not

Ciro,
For some reason your postings seem to be homework:

As you should know, this site does not allow homework here.

Sorry m8........my mistake....it works!!!!!!!!!!!!!!!!
thnx a lot.
1 more question
Id like to show next to the Group the name of the group which the file belngs.
How do i do that?

Ciro,
Every time you are posting, you are proving to us that you are
a student doing homework.
You are completely ignoring the issue.
If you want to post here, you must follow the rules.