0

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.

2
  • read -a assigns values in the array own. By using ${own[@]} you get all array elements in one line. This is the correct behavior... What do you expect exactly? Commented Apr 16, 2018 at 7:57
  • @oliv I'm assuming I've misunderstood what I should be expecting. I want the values read one at a time, not all in one go. The loop only returns one value, which contains all 3 values, rather than returning 3 values. I.e. the loop processes only once, not 3 times as I want it to. Commented Apr 16, 2018 at 8:10

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I started with that, but that gives the same result: $ while read -r own; do echo ${own}":"; done <<< "here I am, rock me like a hurricane" ===> here I am, rock me like a hurricane:
More specifically, just to make the number of iterations clear: while read -r own; do ((ct++)); echo ${own}":"${ct}; done <<< "here I am" ====> here I am:1
@urbanespaceman answer updated, but doing string manipulation in pure bash might lead to problem. If you would describe further what you want to do with result of stat, it is likely that a text processing tool, like sed or awk would do the job better.
That does it! Thanks @oliv. It's only ever intended to read a maximum of about 6 files, and it's going to return an error status if it finds any that aren't owned by the current user (i.e it will compare ${own[@]} with $(whoami) and exit 1 if they're not the same)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.