0

Novice user here.

I have created an environment where there are three users, Tim, James and Harry.

I have created a group called Friends, which includes Tim and James. I have changed the permissions of this group using chmod 0770. Therefore the group has all capabilities (read, write, execute) and anyone outside the group has zero capabilities (i.e. Harry).

I want to automatically test that these permissions work using a script with the Harry user. I've tested it manually on one file called Homework.txt. To do this I entered: nano homework.txt, the permission was denied as expected.

If I want to create a script that carries out the same function but on different files (Photos.txt, Videos.txt, Album.txt) how would I go about doing this? Would be grateful if anyone can shed some light on this.

Thank you

  • 1
    Groups don't have any permissions, files have. To which files or folders did you grant 0770? To shed some light: get a list of files (folders are files too) and iterate over it in a loop. To test if you have access to the files you can touch $file it or cat $file it. – Kev Inski Aug 21 '18 at 04:32
  • Sorry if this is a silly question, but I thought you could assign permissions to users, groups and others. Hence why I used the 0770 permission as 7 gives the group full capabilities (r ,w ,x) and 0 gives others (Harry, in my scenario) no capabilities. There is only one file that is involved at this point and this is the location.txt file @Kev Inski – free_kenpo Aug 21 '18 at 15:04
  • Actually all files have specific permissions and groups use these permissions to get access to the files. But each file knows whom to give access or not. – Kev Inski Aug 22 '18 at 12:18

1 Answers1

2

First, see: How do I create a script file for terminal commands? This covers creating the script itself.

To test whether you can write to a file, you can use:

[ -w filename ]

(And -r for reading, -x for executing.)

To do this for multiple files, you could use a for loop:

for f in Photos.txt Videos.txt Album.txt
do
    if [ -w "$f" ]
    then
        echo "Can write to $f."
    else
        echo "Can't write to $f."
    fi
    if [ -r "$f" ]
    then
        echo "Can read from $f."
    else
        echo "Can't read from $f."
    fi
done

f here is a variable that the shell will set to Photos.txt, Videos.txt and Album.txt in each iteration of the loop. "$f" is how you get the value of the variable.

Olorin
  • 3,488
  • Good answer, you may add some pitfalls like the [ -w filename ] statement needs the spaces otherwise it doesn't work and the error msg is not very helpful or that the variable "$f" in the test statement needs the quotes – Kev Inski Aug 21 '18 at 05:10