1

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?

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Ohh crap - thanks for this, I saw somewhere that someone was listing whole array with just name of array. Thanks a lot
0

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[@]}

Comments

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.