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