I want to read in the owner for a list of files, then compare it to the current owner and output an error if it's not the same.
I have the following, where I've replaced the comparison with a simple echo for simplicity, since it's not the comparison that's going wrong:
while IFS=' ' read -ra own; do echo ${own[@]}; done <<< $(stat -c %U file*.*)
The stat returns a string list (in this case 3 values), all with my username in this case, but the read just outputs it as a single read and string.
<myusername> <myusername> <myusername>
Clarification: I mean the the loop processes once only, returning the string with all 3 values, whereas I want 3 iterations of the loop, containing one value each.
I've changed to IFS= and IFS='\t' in case I was misreadint the output of stat in some way, but I get the same behaviour even if I just define a string like "I am here" instead of the stat command, so I'm obviously doing something else wrong.
Oh, I do need it in a one line statement as well, so if that's the problem then I guess I'm a bit screwed.
In order to get each element of the array, you can loop through the variable own
with a for
loop:
stat -c %U file*.* | { read -ra own; for i in ${own[@]}; do echo $i; done; }
Another way of doing it is:
{ read -ra own; for i in ${own[@]}; do echo $i; done; } <<< $(stat -c %U file*.*)
Note that you don't need a while
loop because you get everything on one line.