Detecting file ownership in Korn shell scripts

I recently was having a play with using a shell script to do some folder creation to help me set up a system for testing and I started to hit ownership issues that caused some shell script errors. At the time, I didn’t realise that there is a test that you can perform for ownership. The "-o" in the code below kicks in the test condition and avoids the error in question.

if [[ -o $dirname ]]
    cd test
    for i in 1 2 3 4 5 6 7 8 9 10
        if [[ -d study$i ]]
            mkdir study$i
    cd ~

Previously, I shared a way to test for directory (-d operator) and file (-f operator) existence that follows the above coding convention. However, there are a plethora of others and I have made a list of them here:

-e fileFile exists
-L fileFile is symbolic link
-r fileUser has read access to file
-s fileFile is non-empty
-w fileUser has write access to file
-x fileUser has execute access to file
-G fileUser’s effective group ID is the same as that of the file
file1 -nt file2File 1 is newer than file2
file1 -ot file2File 1 is older than file2
file1 -et file2File 1 was created at the same time as file2

It’s all useful stuff when you want to rid the command line output of errors in an above board sort of way. These are the kinds of things that often make life easier…

Leave a Reply