Thursday, 15 August 2013

linux - Bash parse dynamic arrays and store specific values in an other -


I am sending a dbus-send command which gives something like this:

  method Return sender =: 1.833 - & gt; Dest =: 1.840 Answer_sialial = 2 bytes [00 01 02 03 04 05] Intel® Boolean Rectangle  

The size of the "bytes" size is dynamic, which may include n values.

I store the results of the dbus-send command in an array:

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

I want to be able to retrieve the values ​​contained in an array of bytes, and if necessary, be able to display one or all of them:

Read Data => 00 01 02 03 04 05 or read the first data & gt; 00

The first data is always accessible by {array [10]} and I think it is possible to use it as a structure:

  IFS = "" "$ {Array [@]: 10}" reads for element in array ...  

Any ideas about completing it?

You really should use some libraries for debuts, like or something similar.

Anyway, for the above example you type:

  #fake dbus-send command dbus-send () {cat & lt; & Lt; EOF method return sender =: 1.833 - & gt; Dest =: 1.840 Answer_sialial = byte of 2 bytes [00 01 02 03 04 05] Intel 1 Boolean true EOF} array = ($ (dbus-send --session --print-repl ..readValue)) data = ($ ( "$ {Array {@}}" | grep -oP 'array \ s * s \ s * bytes \ s * \ [\ s * \ K [^]] * (? = \])' 'Echo' 'echo All data == $ {data [@]} == "resonance" first object: $ {data [0]} "resonance" all items as lines "printf"% s \ n "" $ {data [ ]} "Data = ($ (echo" $ {array {@}} "| sed 's /.* array of bytes [\ [[[^]] * \) \]. * / \ 1 /')) "ALL data == $ {data [@]} ==" resonance "first item: $ {data [ 0]} "echo" all items as lines "printf"% s \ n "" $ {data [@]} / code>  

print for both examples < Pre> all data == 00 01 02 03 04 05 == First item: all items as 00 lines 00 01 02 03 04 05


No comments:

Post a Comment