Reputation: 154
I'm trying to make a script which will find a files, take their dirnames and then go there and do something. I have a problem with adding elements to array, who I want to be my dirnames container.
Code looks like this:
dirnames=()
while read -r line; do
echo "Looking for dirname "$line
dirname=$( dirname $(egrep -lir --include=pom.xml "<name>"$line"</name>" $application_dir))
dirnames+=($dirname)
done < $modules_file
echo "Acquired dirnames"
echo $dirnames
And this is the answer:
Looking for dirname a
Looking for dirname b
Looking for dirname c
Acquired dirnames
/home/user/dev/workspace/a
I have only first dir in my "array". It looks like every another iteration is missing, and i know that these other dirnames are found because of i trying to swap lines.
I was reading a lot about arrays in bash, but everywhere this kind of approach works fine.
Any advice?
Upvotes: 1
Views: 564
Reputation: 7234
I think the problem is with the way you are printing your dirnames array. ${dirnames} will only print the first element. Try printing it like this:
echo ${dirnames[@]}
Upvotes: 0
Reputation: 63471
Bash syntax to expand the entire array is this:
echo ${dirnames[*]}
Or you can access individual elements. e.g.
echo ${dirnames[1]}
Or loop over array:
for d in ${dirnames[*]}; do
echo $d
done
Upvotes: 1