Arrays Bash解析动态数组并将特定值存储在另一个数组中

Arrays Bash解析动态数组并将特定值存储在另一个数组中,arrays,linux,bash,parsing,Arrays,Linux,Bash,Parsing,我正在发送一个dbus send命令,该命令返回如下内容: method return sender=:1.833 -> dest=:1.840 reply_serial=2 array of bytes [ 00 01 02 03 04 05 ] int 1 boolean true “字节数组”大小是动态的,一个can包含n个值 我使用以下命令将dbus send命令的结果存储在数组中: array=($(dbus-send --session

我正在发送一个dbus send命令,该命令返回如下内容:

method return sender=:1.833 -> dest=:1.840 reply_serial=2
   array of bytes [
      00 01 02 03 04 05 
   ]
   int 1
   boolean true
“字节数组”大小是动态的,一个can包含n个值

我使用以下命令将dbus send命令的结果存储在数组中:

array=($(dbus-send --session --print-repl ..readValue))
我希望能够检索包含在字节数组中的值,并能够在必要时显示其中一个或所有值,如下所示:

data read => 00 01 02 03 04 05
or 
first data read => 00
第一个数据总是可以通过{array[10]}访问的,我认为可以使用如下结构:

IFS=" " read -a array 
for element in "${array[@]:10}"
do
    ...
done

关于如何实现这一点,您有什么想法吗?

您真的应该使用一些DBU库,比如或类似的库

无论如何,对于上述示例,您可以编写:

#fake dbus-send command
dbus-send() {
    cat <<EOF
method return sender=:1.833 -> dest=:1.840 reply_serial=2
   array of bytes [
      00 01 02 03 04 05 
   ]
   int 1
   boolean true
EOF
}

array=($(dbus-send --session --print-repl ..readValue))

data=($(echo "${array[@]}" | grep -oP 'array\s*of\s*bytes\s*\[\s*\K[^]]*(?=\])'))

echo "ALL data ==${data[@]}=="
echo "First item: ${data[0]}"
echo "All items as lines"
printf "%s\n" "${data[@]}"

data=($(echo "${array[@]}" | sed 's/.*array of bytes \[\([^]]*\)\].*/\1/'))

echo "ALL data ==${data[@]}=="
echo "First item: ${data[0]}"
echo "All items as lines"
printf "%s\n" "${data[@]}"

另请参见:您使用的是什么版本?因为命令返回grep:invalid选项--“P”Mine:
grep(GNU grep)2.21
-如果您的grep不支持
-P
,请使用第二个
sed
解决方案
ALL data ==00 01 02 03 04 05==
First item: 00
All items as lines
00
01
02
03
04
05