[英]Bash array grouping elements
I'm trying to create an array that looks like this: 我正在尝试创建一个看起来像这样的数组:
0 kernel
1 /boot/vmlinuz-4.8.0-1.el7.elrepo.x86_64
2 kernel
3 /boot/vmlinuz-4.7.3-1.el7.elrepo.x86_64
Using the grubby command of which the ouput for the entires I'm looking for are: 使用肮脏的命令,我正在寻找的输出是:
index=2
kernel=/boot/vmlinuz-4.8.0-1.el7.elrepo.x86_64
root=/dev/mapper/<my_vol>-root
initrd=/boot/initramfs-4.8.0-1.el7.elrepo.x86_64.img
title=CentOS Linux (4.8.0-1.el7.elrepo.x86_64) 7 (Core)
index=3
kernel=/boot/vmlinuz-4.7.3-1.el7.elrepo.x86_64
root=/dev/mapper/<my_vol>-root
initrd=/boot/initramfs-4.7.3-1.el7.elrepo.x86_64.img
title=CentOS Linux (4.7.3-1.el7.elrepo.x86_64) 7 (Core)
(I've removed the "args=" line for sanitisation purposes) (出于卫生目的,我删除了“ args =”行)
When I try to use the following command which works... (err... for the first 2 elements of the array). 当我尝试使用下面的命令时...(错误...用于数组的前2个元素)。 It decided that 2 indicies should be squashed into one.
它决定将2个索引压缩为1个。
IFS='=' read -a KERNELS <<< $(sudo grubby --info=ALL |grep -v rescue |grep -E 4.[78] |grep kernel)
0 kernel
1 /boot/vmlinuz-4.8.0-1.el7.elrepo.x86_64 kernel
2 /boot/vmlinuz-4.7.3-1.el7.elrepo.x86_64
First of why is it doing this? 首先,为什么要这样做? And how can I fix it?
我该如何解决?
I understand that there are other ways I could solve this problem but I want to understand what's going on here: 我了解可以通过其他方法解决此问题,但我想了解此处发生的情况:
for i in ${KERNELS[@]}; do echo $i; done
kernel
/boot/vmlinuz-4.8.0-1.el7.elrepo.x86_64
kernel
/boot/vmlinuz-4.7.3-1.el7.elrepo.x86_64
for i in ${!KERNELS[@]}; do echo $i ${KERNELS[$i]}; done
0 kernel
1 /boot/vmlinuz-4.8.0-1.el7.elrepo.x86_64 kernel
2 /boot/vmlinuz-4.7.3-1.el7.elrepo.x86_64
I suspect it has something to do with the fact that when you run 我怀疑这与您跑步时的事实有关
sudo grubby --info=ALL |grep -v rescue |grep -E 4.[78] |grep kernel
kernel=/boot/vmlinuz-4.8.0-1.el7.elrepo.x86_64
kernel=/boot/vmlinuz-4.7.3-1.el7.elrepo.x86_64
You get two lines of output and the IFS= is separating on '=' and ignoring the '\\n' 您得到两行输出,并且IFS =在'='上分开并忽略'\\ n'
I've tried using awk '{print}' ORS=' '
ideas to output everything on one line but it gets messy. 我已经尝试使用
awk '{print}' ORS=' '
想法在一行上输出所有内容,但会变得混乱。
There has to be a more elegant way to use the hereto string with multiple lines of output. 必须有一种更优雅的方法来将hereto字符串用于多行输出。 Surely...
一定...
You can use process substitution combined with read
directive: 您可以将进程替换与
read
指令结合使用:
arr=()
while IFS='=' read -r k v; do
[[ $k = "kernel" ]] && { arr+=("$k"); arr+=("$v"); }
done < <(sudo grubby --info=ALL)
Check array content: 检查数组内容:
printf '[%s]\n' "${arr[@]}"
[kernel]
[/boot/vmlinuz-4.8.0-1.el7.elrepo.x86_64]
[kernel]
[/boot/vmlinuz-4.7.3-1.el7.elrepo.x86_64]
Ok so I accidently just has a moment of clarity using the exta info posted above 好的,所以我偶然使用以上发布的exta信息来澄清一下
IFS='=' read -a KERNELS <<< $(sudo grubby --info=ALL |grep -v rescue |grep -E 4.[78] |grep kernel | awk '{print}' ORS='=')
for i in ${!KERNELS[@]}; do echo ${KERNELS[$i]} $i; done
kernel 0
/boot/vmlinuz-4.8.0-1.el7.elrepo.x86_64 1
kernel 2
/boot/vmlinuz-4.7.3-1.el7.elrepo.x86_64 3
I've used awk to get around the fact that there are multiple lines with terminating '\\n' and instead inserted a '=' which will be the delimiting factor in building the array. 我用awk解决了以下事实:有多行以'\\ n'结尾,而是插入了'=',这将是构建数组的定界因素。 =]
=]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.