Bash脚本中的数组长度为1 [英] Array Length is 1 in Bash scripting
本文介绍了Bash脚本中的数组长度为1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下面的代码中,数组长度为1.
In the below code, the array length is 1.
任何人都可以解释为什么,因为grep输出将显示在每行中,但是当存储在数组中时,数组长度将为1.
Could anyone explain why, as grep output will displayed in each new line but when it is stored in the array, the array length will be 1.
如何显示读取数组的每一行?
How to display each line reading the array?
#!/bin/bash
NUM=()
SHORT_TEXT=()
LONG_TEXT=()
#cat /tmp/dummy2 |
while read NUM
do
LONG_TEXT+=$(grep $NUM -A4 RtpLogShm.Msg | grep -vi abate | grep ^LG)
done < /tmp/dummy2
#cat /tmp/dummy1 |
while read LINE
do
NUM+=$(echo $LINE | awk -F':' '{print $1}')
SHORT_TEXT+=$(echo $LINE | awk -F':' '{print $2}')
done < /tmp/dummy1
printf "[%s]\n" "${LONG_TEXT[@]}"
done
done
推荐答案
在bash中,追加到数组的语法是(例如,我们想将存储在 $ {new_element}
中的元素追加到现有数组 $ {array [@]}
):
In bash, the syntax of appending to an array is (say we want to append an element stored in ${new_element}
to an existing array ${array[@]}
):
array=("${array[@]}" "${new_element}")
这篇关于Bash脚本中的数组长度为1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文