Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
i have to write a script that 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
how do i read the peermission information from a file and write it like the example above?Please guys i need your help

I won't do the whole script for you, but I'll get you started. The first column of the a long listing contains all the permissions for owner, group and everybody else.
After finding the permissions, parse them to get each individual permission. This shows the owner:
#!/bin/bash
fperm=$(ls -l myfile|awk ' { print $1 } ')
echo $fperm
or=$(echo "$fperm"|cut -c2)
echo $or # owner read
ow=$(echo "$fperm"|cut -c3)
echo $ow # owner write
oe=$(echo "$fperm"|cut -c4)
echo $oe # owner execute

Thnx a lot mate i really appreciate your interest, but
i ve been told to use less then 15 lines of code and 10 are only for the owner inthe one u gave me...is there another way of doing it?

Well, make it in one line (long one though):
find ./ -mindepth 1 -ls | awk '\
BEGIN{print "Read", "Write", "eXecute"} \
{\
if (substr($3,1,1)=="d") printf "\n\n folder %s", $11; else printf "\n\n file %s",$11; \
printf "\nOwner %s", $5 ;\
if (substr($3,2,1)=="r") printf " YES"; else printf " NO "; \
if (substr($3,3,1)=="w") printf " YES"; else printf " NO ";\
if (substr($3,4,1)=="x") printf " YES"; else printf " NO "; \
printf "\nGroup %s", $6; \
if (substr($3,5,1)=="r") printf " YES"; else printf " NO ";\
if (substr($3,6,1)=="w") printf " YES"; else printf " NO "; \
if (substr($3,7,1)=="x") printf " YES"; else printf " NO ";\
printf "\nRestOfWorld "; \
if (substr($3,8,1)=="r") printf " YES"; else printf " NO ";\
if (substr($3,9,1)=="w") printf " YES"; else printf " NO "; \
if (substr($3,10,1)=="x") printf " YES"; else printf " NO ";\
}'

to be honest im a bit confused!!!
i tried to run it but there are lots of mistake that im not able to correct

>i tried to run it but there are lots of mistake that im not able to correct
did you run that form shell? don't.
1. put this 'line' as is into some file, save it and run as
[prompt]>sh yourfile
it should work
2. add as a first line #!/bin/sh, then "chmod u+x yuorfile"; script is ready. Done. Tell back what you see.P.S. shell treats 'new line' simbols as one of possible delimiter of instructions. Unless you put '\' on the end! than, even if it looks like bunch of lines, shell treats it as one huge long command line.

look at this...would you be able to work out how to use the if statements to dispalay the words yes or no?
[clive@vle ~]$ ls -la .bashrc
-rw-r--r-- 1 clive clive 155 Jul 17 2005 .bashrc
[clive@vle ~]$ ls -la .bashrc | cut -c1
-
[clive@vle ~]$ ls -la .bashrc | cut -c2
r
[clive@vle ~]$ ls -la .bashrc | cut -c3
w
[clive@vle ~]$ ls -la .bashrc | cut -f3 -d" "
1
[clive@vle ~]$ ls -la .bashrc | cut -f4 -d" "
clive

Yes, please:
master@console:~$ ls -la .bashrc | cut -c1 | awk '{if ($1 != "-") print "Yes"; else print "No";}'
No
master@console:~$ ls -la .bashrc | cut -c1 | awk '{if ($1 == "-") print "Yes"; else print "No";}'
YesAlso:
master@console:~$ cat yourfile
find ./ -mindepth 1 -ls | awk '\
BEGIN{print "Read", "Write", "eXecute"} \
{\
if (substr($3,1,1)=="d") printf "\n\n folder %s", $11; else printf "\n\n file %s",$11; \
printf "\nOwner %s", $5 ;\
if (substr($3,2,1)=="r") printf " YES"; else printf " NO "; \
if (substr($3,3,1)=="w") printf " YES"; else printf " NO ";\
if (substr($3,4,1)=="x") printf " YES"; else printf " NO "; \
printf "\nGroup %s", $6; \
if (substr($3,5,1)=="r") printf " YES"; else printf " NO ";\
if (substr($3,6,1)=="w") printf " YES"; else printf " NO "; \
if (substr($3,7,1)=="x") printf " YES"; else printf " NO ";\
printf "\nRestOfWorld "; \
if (substr($3,8,1)=="r") printf " YES"; else printf " NO ";\
if (substr($3,9,1)=="w") printf " YES"; else printf " NO "; \
if (substr($3,10,1)=="x") printf " YES"; else printf " NO ";\
}'master@console:~$ sh yourfile | head -10
Read Write eXecute
folder ./.gimp-2.2
Owner master YES YES YES
Group master YES NO YES
RestOfWorld YES NO YESfolder ./.gimp-2.2/brushes
Owner master YES YES YES

#!/bin/sh
for i in `ls`
do
OWNER=`ls -l $i | awk '{print $3}'`
GROUP=`ls -l $i | awk '{print $4}'`
OTHERS="Restofworld";
TEMP=`ls -l $i | cut -c 2-10`
j=1
while [ $j -le 9 ]
do
if [ `echo $TEMP | cut -c $j` != "-" ];then
WORD="YES"
else
WORD="NO"
fi
if [ $j -le 3 ];then
OWNER="$OWNER $WORD"
elif [ $j -gt 3 -a $j -le 6 ];then
GROUP="$GROUP $WORD"
else
OTHERS="$OTHERS $WORD"
fi
let j=$j+1
done
ls -l $i
echo "$OWNER"
echo "$GROUP"
echo "$OTHERS"
done
shivakumara K Madegowda

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |