[英]Bash scripting & array pushing
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. 我在bash上阅读了很多有关数组的内容,但是在任何地方这种方法都可以正常工作。
Any advice? 有什么建议吗?
Bash syntax to expand the entire array is this: Bash扩展整个数组的语法是这样的:
echo ${dirnames[*]}
Or you can access individual elements. 或者,您可以访问单个元素。 eg
例如
echo ${dirnames[1]}
Or loop over array: 或循环遍历数组:
for d in ${dirnames[*]}; do
echo $d
done
I think the problem is with the way you are printing your dirnames array. 我认为问题在于您打印目录名数组的方式。 ${dirnames} will only print the first element.
$ {dirnames}将仅打印第一个元素。 Try printing it like this:
尝试像这样打印:
echo ${dirnames[@]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.