0

What I want to do:

Go through every sub folder of some base folder, if ANY file or folder exists that does not have "my_user" as owner. Return false

#!/bin/bash
checkOwnerShipOfFiles () {
    files=$1
    for f in $files; do
            echo "$f"
            if [ ! -d "$f" ]; then
                    owner=$(stat -c %U "$f")
                    echo "$owner"
                    if [ "$owner" != "my_user"  ]; then
                            echo "Invalid owner: $owner of file $f"
                            return 255;
                    fi
            else
                    check=$(checkOwnerShipOfFiles "$f/*")
                    echo "check: $check"
                    if [ "$check" = false ]; then false; fi;
            fi
    done
    return 0
}

response=$(checkOwnerShipOfFiles "/test/base")
echo "response: $response"
if [ "$response" = "0"  ];
    then
        echo "success result"
        return 0;
    else
        echo "error result"
        return 255;
fi

Shellcheck.net has no issues with this code, and it runs fine. But does not output what I expect. the variable check seems to return the string that I use as input, but I only return 0 or 255. check HAS to be one of those values, no?

My specific issue was as @Charles Duffey suggested, that I used $(functionCall) which returns the echo of that function, while I wanted the return value.

The script that ended up working:

#!/bin/bash
checkOwnerShipOfFiles () {
    files=$1
    for f in $files; do
            if [ ! -d "$f" ]; then
                    owner=$(stat -c %U "$f")
                    if [ "$owner" != "my_user"  ]; then
                            echo "Wrong user: $owner, is owner of: $f"
                            return 255;
                    fi;
            else
                    if ! checkOwnerShipOfFiles "$f/*"; then
                            return 255;
                    fi;
            fi
    done
    return 0
}

if checkOwnerShipOfFiles "/test/base";
    then
        return 0;
    else
        #echo "There exists at least 1 file that does not have the correct user permission";
        return 255;
fi
Nixxon
  • 767
  • 1
  • 11
  • 24
  • 1
    Run your code through shellcheck.net first, so we don't need to waste time fixing the obvious problems. – chepner Apr 30 '18 at 14:49
  • Easy to ask `find` to list files with UID not matching a desired value -- that you you only have *one* external command for the whole script, rather than one `stat` (run in a command substitution, thus adding even more runtime cost) per file. – Charles Duffy Apr 30 '18 at 16:00
  • BTW, if you think this code "runs fine", you haven't yet used it with directories with spaces in their names. – Charles Duffy Apr 30 '18 at 16:01
  • Also, you're confusing return value (which goes in `$?`) with output (which is captured by `$(...)`). `foo=$(bar)` doesn't capture what `bar` *returns*, but what it *emits on stdout*. – Charles Duffy Apr 30 '18 at 16:02
  • @CharlesDuffy Indeed the specific issue I had was using $() which captures the echo of the method, and not the return value. I've edited the question to include the script that ended up working for me, which is a correct answer to the question I originally posed. – Nixxon May 02 '18 at 08:34
  • This is still quite buggy -- try using it with a directory that contains spaces. `"$f"/*` and using `"$@"` instead of `$1` would be a step towards a fix, but that's still far slower than letting `find` do all the work. – Charles Duffy May 02 '18 at 13:50

1 Answers1

1

Why reinvent the wheel? The mtree program is designed to check ownership and permissions of a directory tree. It follows the Unix philosophy of having one tool to do one thing well.

Jens
  • 69,818
  • 15
  • 125
  • 179