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 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,548