[英]How can I assign the output of a function with parameters to a variable using bash?
[英]How can I assign output to two bash arrays variables
这里大概有类似的答案,但是没有什么可以真正回答我的问题。 我在bash脚本中的某个点必须从输出中填充两个数组,如下所示:
part-of-the-file1:line_32
part-of-the-file1:line_97
part-of-the-file2:line_88
part-of-the-file2:line_93
我需要做的是在各自独立的数组中提取文件和行号。 到目前为止,我有:
read FILES LINES <<<($(echo $INPUTFILES | xargs grep -n $id | cut -f1,2 -d':' | awk '{print $1 " " $2}'
并具有经过修改的IFS=':'
但我肯定没有更好的解决方案,因为我不是脚本或工具向导。
read
不能一次填充两个数组 ,就像它可以使用单词拆分填充两个常规参数一样。 最好只迭代管道的输出以构建阵列。
while IFS=: read fname lineno therest; do
files+=("$fname")
lines+=("$lineno")
done < <(echo $INPUTFILES | xargs grep -n "$id")
在这里,我让read
进行grep
输出尾部的拆分和丢弃,以代替使用cut
和awk
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.