Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Arrays in Bash and running stat against each directory

I have an unknown set of files/folders (I keep having to add to the list) that I need to check for owner and permissions. So I pieced together

dirs=(/mnt /mnt/data /mnt/data/1.txt)
for i in $(dirs[@]}; do
stat -c "%U:$G" ${dirs[$i]}
done

However, I receive a syntax error ‘operand expected (error token is "/mnt"’

point me in the right direction please

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

for i in ${dirs[@]} isn’t iterating the indices of the array, but the actual contents.

Try this:

dirs=(/mnt /mnt/data /mnt/data/1.txt)
for i in "${dirs[@]}"; do
stat -c "%U:$G" "$i"
done

EDIT With cleaner variable names per glennjackman’s suggestion:

paths=(/mnt /mnt/data /mnt/data/1.txt)
for path in "${paths[@]}"; do
stat -c "%U:$G" "$path"
done
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading