First, you shouldn't parse the output of ls
and its variations. You can go about this using stat
:
$ stat -c%U-%G ./*
tomasz-tomasz
tomasz-tomasz
tomasz-tomasz
As you can see, the result is a reliable list of two words concatenated, which you can operate on to get the result wanted. Put it into a loop, and there you go:
PASS=true
for i in $(stat -c%U-%G ./*); do
if ! [[ "$i" == root-root ]]; then
PASS=false; break
fi
done
if "$PASS"; then
echo Pass
else
echo Fail
fi
The value of i
needs to be root-root
for the loop to get to its end with the switch unchanged.
Replace ./*
with the_dir/*
to test another location.
The -
separator is needed, because, as Grump noted in the comments, The string comparison may fail if the file is owned by 'roo' and in the group 'troot', so a separator would still be a good thing.
Familiarise yourself with this: Why *not* parse `ls` (and what do to instead)?