用空格循环数组字符串 [英] Loop through array of arrays of string with spaces
问题描述
我试图循环一个包含其他数组的数组,这些数组由包含空格的字符串组成。问题是,我似乎无法保留字符串中的间距。如果我将IFS更改为\\\
,或者将数组的所有元素都视为1项,如果我将IFS保持不变,则这些字符串会被分成多个项目:
#!/ bin / sh
low1 =(AA QQBB LL)
low2 =(CCDD)
low3 =(EEFF)
high =(low1 low2 low3)
for high_item in $ {high [@]}
do
eval arrayz = \ $ {$ high_item [@]}
#IFS = $'\\\
'
for $ arrayz
echo $ item
完成
完成
输出:
AA
BB
LL
CC
DD
EE
FF
您可以看到元素AA QQ和BB LL已被分割。
如果我取消注释将 \\\
的行,我得到以下结果:
AA QQ BB LL
CC DD
EE FF
现在将AA QQ和BB LL连接在一起!
有没有反正我可以保留这些元素,就像他们原来的一样...我需要的输出看起来像这样:
AA QQ
BB LL
CC
DD
EE
FF
我认为你的意思是输出应该是这样的:
AA QQ
BB LL
CC
DD
EE
FF
$ {low1 [0]}
$ {low1 [1]}
$ {low2 [0]}
$ {low2 [1]}
$ {low3 [0]}
$ {low3 [1]}
可以使用以下方式来完成:
#!/ bin / bash
low1 =(AA QQBB LL)
low2 =(CCDD)
low3 =(EEFF)
high =(low1 low2 low3)
为high_item in $ {high [@]}
do
x = $ {high_item} [@]# - > $ {array [']
arrays =($ {!x})
#IFS = $'\ n'
@]}
do
echo$ item
done
done
和请 总是使用 解释: 为了满足我们的需要, 你必须在数组上下文中进行评估。否则,它不会像预期的那样工作(如果你做了 嗯,最后说明: I'm trying to loop through an array that contains other arrays and these arrays consist of strings with spaces. The problem is that I can't seem to preserve the spacing in the string. The string with spaces are either divided into multiple items if I change IFS to \n or all the elements of the array are seen as 1 item if I leave IFS unchanged here's some sample code: Output: As you can see the elements "AA QQ" and "BB LL" have been split. If I uncomment the line that sets Now "AA QQ" and "BB LL" are concatenated! Is there anyway I can preserve these elements just as they original are...I need the output to look like that:
I think you meant that the output should look like: i.e.: This could be accomplished using: And please always use Explanation: For our needs, And you have to evaluate it in array context; otherwise, it wouldn't work as expected (if you do Ah, and as final note: 这篇关于用空格循环数组字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!#!/ bin / bash
作为bash脚本。 b $ b $ {!x}
是间接变量扩展。它的计算结果是一个包含在 $ x
中的变量的值。
x
还需要使用 [@]
后缀进行数组扩展。尤其要注意的是它是 x = $ {high_item} [@]
和不是 x = $ {high_item [@] }
。
arrays = $ {!x}
)。
IFS
在这里没有任何区别。只要你在引用的数组上工作, IFS
不会起作用。#!/bin/sh
low1=("AA QQ" "BB LL")
low2=("CC" "DD")
low3=("EE" "FF")
high=(low1 low2 low3)
for high_item in ${high[@]}
do
eval arrayz=\${$high_item[@]}
#IFS=$'\n'
for item in $arrayz
do
echo $item
done
done
AA
QQ
BB
LL
CC
DD
EE
FF
IFS
to \n
I get the following:
AA QQ BB LL
CC DD
EE FF
AA QQ
BB LL
CC
DD
EE
FF
AA QQ
BB LL
CC
DD
EE
FF
${low1[0]}
${low1[1]}
${low2[0]}
${low2[1]}
${low3[0]}
${low3[1]}
#!/bin/bash
low1=("AA QQ" "BB LL")
low2=("CC" "DD")
low3=("EE" "FF")
high=(low1 low2 low3)
for high_item in ${high[@]}
do
x="${high_item}[@]" # -> "low1[@]"
arrays=( "${!x}" )
#IFS=$'\n'
for item in "${arrays[@]}"
do
echo "$item"
done
done
#!/bin/bash
for bash scripts.${!x}
is indirect variable expansion. It evaluates to the value of variable with a name contained in $x
.x
needs to have the [@]
suffix for array expansion as well. Especially note that it is x=${high_item}[@]
and not x=${high_item[@]}
.arrays=${!x}
).IFS
doesn't make any difference here. As long as you are working on quoted arrays, IFS
doesn't come into play.