[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

A Little Help

i have a quick question

how do i write a script named 'permissions' 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.

i think that this has something to do with $ chmod 777 [file] who is used to determine the permission of the owne, group  and world. i know the 777 will give read, write and execute permissions to all three groups, but i don't know how do write the script.

can someone help me??
0
FirstMan
Asked:
FirstMan
  • 6
  • 2
1 Solution
 
chingmdCommented:
Can you explain a little further.

ls -l <file>  will show you the permissions of the owner, group, and other.  As well as show you the owner and the group ownership.  
This is an excerpt from http://www.dartmouth.edu/~rc/help/faq/permissions.html

An example of the output produced by 'ls -l' is shown below.

drwx------ 2 richard staff  2048 Jan  2 1997  private
drwxrws--- 2 richard staff  2048 Jan  2 1997  admin
-rw-rw---- 2 richard staff 12040 Aug 20 1996  admin/userinfo
drwxr-xr-x 3 richard user   2048 May 13 09:27 public

Understanding how to read this output is useful to all unix users, but especially people using group access permissions.

Field 1:   a set of ten permission flags.
Field 2:   link count (don't worry about this)
Field 3:   owner of the file
Field 4:   associated group for the file
Field 5:   size in bytes
Field 6-8: date of last modification (format varies, but always 3 fields)
Field 9:   name of file (possibly with path, depending on how ls was called)

To find out the members of a group, you can type id <username>  or groups <username>

To find the members of a group, you can cat /etc/group | grep <groupname>
or use ypcat if you are an an NIS environment.  

In a single command, you'd want to script that.  Is that what you are asking for?

0
 
FirstManAuthor Commented:
ok i will try explain that further..

i want to write a script called 'permissions' which takes a file as an argument. the script (permissions) should be able to determine which permissions the owner, group and everybody has for the file passed in. after this is completed, the output should resemble this  

                                          READ WRITE EXECUTE
                                          -------  -------- ------------
        owners <usename> yes      yes        no  
        group users              yes       no        no
        everybody                 no        no        no




0
 
chingmdCommented:
This looks like a homework assignment.    Do you have an language requirements?

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
chingmdCommented:
I just chose bash.   perl is another good one.   Php is also good, althought a lot of people seem to think it's only good for web usage.

#!/bin/bash

perm=$(ls -l $1 | cut -f1 -d" ")
group=$(ls -l $1 | cut -b26-32 )
owner=$(ls -l $1 | cut  -b17-23)
or=$(echo $perm | cut -b2)
ow=$(echo $perm | cut -b3)
ox=$(echo $perm | cut -b4)
gr=$(echo $perm | cut -b5)
gw=$(echo $perm | cut -b6)
gx=$(echo $perm | cut -b7)
er=$(echo $perm | cut -b8)
ew=$(echo $perm | cut -b9)
ex=$(echo $perm | cut -b10)

echo "For file: $1"
echo -e "\t\tREAD\tWRITE\tEXEC"
echo -e "\t\t----\t-----\t----"

echo -e "$owner\t\t$or\t$ow\t$ox"
echo -e "$group\t\t$gr\t$gw\t$gx"
echo -e "other\t\t$er\t$ew\t$ex"

0
 
FirstManAuthor Commented:
Thank you for your assistance. i have done the question but the results are abit funny though.

the output is supposed to resemble this:

                                              READ WRITE EXECUTE
                                              -------  -------- ------------
        owners adam.adebisi     yes      yes        no  
        group users                    yes       no        no
        everybody                       no        no        no

But the results resemble this:

For file:
                READ    WRITE   EXEC
                ----    -----   ----

m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi
m.adebi         o       t       a

 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users
 users          l               -
other           r       w       -

what went wrong? or is it supposed to be look this??
0
 
chingmdCommented:
Nope.

Let me cut and paste this back out and see if it changes.
0
 
chingmdCommented:
I had to modify the code a littel bit to make it work on the machine I am on now.

but this was the output.  

For file: test1.sh
                READ    WRITE   EXEC
                ----    -----   ----
me              r       w       x
users          r       -       x
other           r       -       x

Here is the code.


#!/bin/bash

perm=$(ls -l $1 | cut -f1 -d" ")
group=$(ls -l $1 | cut -f4 -d" " )
owner=$(ls -l $1 | cut  -f3 -d" ")
or=$(echo $perm | cut -b2)
ow=$(echo $perm | cut -b3)
ox=$(echo $perm | cut -b4)
gr=$(echo $perm | cut -b5)
gw=$(echo $perm | cut -b6)
gx=$(echo $perm | cut -b7)
er=$(echo $perm | cut -b8)
ew=$(echo $perm | cut -b9)
ex=$(echo $perm | cut -b10)

echo "For file: $1"
echo -e "\t\tREAD\tWRITE\tEXEC"
echo -e "\t\t----\t-----\t----"

echo -e "$owner\t\t$or\t$ow\t$ox"
echo -e "$group\t\t$gr\t$gw\t$gx"
echo -e "other\t\t$er\t$ew\t$ex"


ls -l test1.sh  has this output.

-rwxr-xr-x 1 me users 547 2007-10-07 00:29 test1.sh

0
 
chingmdCommented:
So, what OS are you on?   And what version of bash are you running?   As well as what is the output of ls -l on that machine.

The machine before(red hat), used ls in a fixed width format, and the ubuntu box at home uses space delimited.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now